📖チュートリアル一覧▼

Vue 3 (Composition API 統合)

Vue 3

Vue 3 と Strelit UI Kit の統合は、

<script setup>

と Vue 標準の

<Teleport>

を使用することで、クリーンかつ直感的に実現できます。

1. インストール

Vue 3 プロジェクトに Strelit UI Kit パッケージをインストールします:

npm install strelit-ui-kit

2. ドッキングワークスペースコンポーネント (DockingWorkspace.vue)

Vue 3 では、<Teleport> を使用して、Strelit Layout によって作成された DOM コンテナにリアクティブな Vue コンポーネントを直接投影できます:

<script setup lang="ts">
import { ref, onMounted, onUnmounted, reactive } from 'vue';
import { StrelitLayout, LayoutConfig, ComponentContainer } from 'strelit-ui-kit';
import 'strelit-ui-kit/dist/css/strelit-base.css';
import 'strelit-ui-kit/dist/css/themes/strelit-dark-theme.css';

import TelemetryViewer from './TelemetryViewer.vue';
import LogStreamViewer from './LogStreamViewer.vue';

interface PortalSlot {
  id: string;
  target: HTMLElement;
  type: 'telemetry' | 'logs';
}

const layoutContainer = ref<HTMLElement | null>(null);
let layoutInstance: StrelitLayout | null = null;
const activeSlots = reactive<PortalSlot[]>([]);

onMounted(() => {
  if (!layoutContainer.value) return;

  const config: LayoutConfig = {
    root: {
      type: 'row',
      content: [
        {
          type: 'component',
          componentType: 'vuePortal',
          title: 'ライブテレメトリ',
          componentState: { slotType: 'telemetry', id: 'slot-telemetry' }
        },
        {
          type: 'component',
          componentType: 'vuePortal',
          title: 'イベントストリーム',
          componentState: { slotType: 'logs', id: 'slot-logs' }
        }
      ]
    }
  };

  // 1. コンテナ要素上でレイアウトを初期化
  layoutInstance = new StrelitLayout(layoutContainer.value);

  // 2. Vue コンポーネントファクトリ関数を登録
  layoutInstance.registerComponentFactoryFunction('vuePortal', (container: ComponentContainer, itemConfig) => {
    const el = container.element;
    const state = (itemConfig.componentState as { slotType: 'telemetry' | 'logs'; id: string }) || { slotType: 'telemetry', id: 'slot-default' };

    activeSlots.push({
      id: state.id,
      target: el,
      type: state.slotType
    });

    container.on('destroy', () => {
      const idx = activeSlots.findIndex(s => s.id === state.id);
      if (idx !== -1) activeSlots.splice(idx, 1);
    });
  });

  // 3. レイアウト構成をロード
  layoutInstance.loadLayout(config);

  const handleResize = () => layoutInstance?.updateRootSize();
  window.addEventListener('resize', handleResize);
});

onUnmounted(() => {
  layoutInstance?.destroy();
});
</script>

<template>
  <div class="workspace-viewport">
    <div ref="layoutContainer" class="layout-target"></div>

    <!-- リアクティブな Vue コンポーネントを Strelit コンテナに Teleport -->
    <Teleport v-for="slot in activeSlots" :key="slot.id" :to="slot.target">
      <TelemetryViewer v-if="slot.type === 'telemetry'" />
      <LogStreamViewer v-else-if="slot.type === 'logs'" />
    </Teleport>
  </div>
</template>

<style scoped>
.workspace-viewport {
  width: 100vw;
  height: 100vh;
  position: relative;
  overflow: hidden;
}
.layout-target {
  width: 100%;
  height: 100%;
}
</style>

3. Vue 3 Teleport パターンの利点

  • リアクティビティの保持: Pinia ストア、Vue 3 の ref 、および算出プロパティ(computed)は、再初期化されることなく更新を継続します。

  • Scoped CSS の適用: .vue 単一ファイルコンポーネントのスコープ付きスタイルがそのまま動作します。

  • クリーンな破棄処理: Strelit からパネルを閉じると、標準の Vue ライフサイクルクリーンアップが自動的に実行されます。

マルチ画面トレーディング端末やエンタープライズ分析ダッシュボードの構築、レガシーレイアウトからの移行など、チームとコミュニティがサポートします。

GitHub で Issue を作成 →