モダンな Angular は、Standalone Components、Signals、そして
createComponent と ViewContainerRef を介した直接的な DOM
注入を活用します。このガイドでは、Strelit UI Kit
とのクリーンでボイラープレートのない統合手法を解説します。
1. インストール
Angular プロジェクトの依存関係に Strelit UI Kit を追加します:
npm install strelit-ui-kit
2. Standalone ドッキングホストコンポーネント
Strelit を初期化し、スタンドアロン Angular コンポーネントを Strelit コンテナに動的にマウントする Angular コンポーネントを作成します:
import {
Component,
ElementRef,
viewChild,
OnInit,
OnDestroy,
ApplicationRef,
createComponent,
EnvironmentInjector,
Type,
} from '@angular/core';
import { StrelitLayout, LayoutConfig, ComponentContainer } from 'strelit-ui-kit';
import { TelemetryPanelComponent } from './telemetry-panel.component';
import { LogStreamPanelComponent } from './log-stream-panel.component';
@Component({
selector: 'app-docking-workspace',
standalone: true,
template: `<div #dockingContainer class="docking-viewport"></div>`,
styles: [
`
.docking-viewport {
width: 100vw;
height: 100vh;
overflow: hidden;
}
`,
],
})
export class DockingWorkspaceComponent implements OnInit, OnDestroy {
private containerEl = viewChild.required<ElementRef<HTMLDivElement>>('dockingContainer');
private layout?: StrelitLayout;
private registry: Record<string, Type<any>> = {
telemetry: TelemetryPanelComponent,
logs: LogStreamPanelComponent,
};
constructor(
private appRef: ApplicationRef,
private injector: EnvironmentInjector
) {}
ngOnInit() {
const config: LayoutConfig = {
root: {
type: 'row',
content: [
{
type: 'component',
componentType: 'angularHost',
title: 'テレメトリインスペクター',
componentState: { componentKey: 'telemetry' },
},
{
type: 'component',
componentType: 'angularHost',
title: 'イベントログストリーム',
componentState: { componentKey: 'logs' },
},
],
},
};
// 1. 対象の DOM 要素上で直接 StrelitLayout を初期化
this.layout = new StrelitLayout(this.containerEl().nativeElement);
// 2. 動的な Angular コンポーネントファクトリを登録
this.layout.registerComponentFactoryFunction(
'angularHost',
(container: ComponentContainer, itemConfig) => {
const targetEl = container.element;
const state = (itemConfig.componentState as { componentKey: string }) || {
componentKey: '',
};
const ComponentClass = this.registry[state.componentKey];
if (!ComponentClass) return;
// スタンドアロンコンポーネントを動的に生成
const compRef = createComponent(ComponentClass, {
environmentInjector: this.injector,
hostElement: targetEl,
});
this.appRef.attachView(compRef.hostView);
// ペインが閉じられたときに Angular コンポーネントのライフサイクルを破棄
container.on('destroy', () => {
this.appRef.detachView(compRef.hostView);
compRef.destroy();
});
}
);
// 3. レイアウト構成をロード
this.layout.loadLayout(config);
}
ngOnDestroy() {
this.layout?.destroy();
}
}
3. Angular Signals によるリアクティブな状態管理
スタンドアロンパネルの内部では、Angular Signals を自由に使用できます。状態変更は不要なグローバル変更検知サイクルを発生させることなく、ドッキングされたパネル内で即座に再レンダリングされます:
import { Component, signal, computed } from '@angular/core';
@Component({
selector: 'app-telemetry-panel',
standalone: true,
template: `
<div class="telemetry-box">
<h4>スループット: {{ throughput() }} req/s</h4>
<p>ステータス: {{ statusLabel() }}</p>
<button (click)="increment()">トラフィックスパイクをシミュレート</button>
</div>
`,
})
export class TelemetryPanelComponent {
throughput = signal(1240);
statusLabel = computed(() => (this.throughput() > 2000 ? '高負荷 🔥' : '正常 🟢'));
increment() {
this.throughput.update((v) => v + 250);
}
}
レガシーな AngularJS 1.x のチュートリアルをお探しですか?
$scope を使用する AngularJS 1.x
のコードベースを維持管理している場合は、アーカイブセクションの
AngularJS (シンプル) および
AngularJS (マルチビュー) ガイドを参照してください。