Runtime API / Method

FlamoInstance.mountChild

向当前父 Instance 的运行树新增一个具有稳定 key 的 child,并以 composition transaction 提交。

使用说明

新增一个 child

mountChild() 保留现有 children,并在完整 desired set 中加入一个新的 RuntimeChildSpec。同一个父级不能重复使用相同 key。

这里的 mount 是“把 child 加入当前父 Instance 的运行图”,不是宿主 DOM 挂载。它不会改变父 Instance 的 target 或 canvas,也不是不存在的 instance.mount(target)。

const receipt = await panel.mountChild({
key: 'empty-state',
artifact: EmptyState({ message: '暂无结果' }),
parentKey: null,
mount: { kind: 'root' },
props: { compact: true },
statePolicy: 'preserve',
});
if (receipt.status !== 'committed') {
throw new Error(receipt.error?.message ?? `mountChild: ${receipt.status}`);
}
const child = panel.child('empty-state');

spec 必须且只能提供 ref、artifact、instance 其中一种:

  • ref:通过 Runtime 的 componentResolver 解析精确 Component Release;
  • artifact:创建 Runtime-owned 的本地 child;
  • instance:挂接已经存在的 live Instance,默认保持 caller-owned。

挂接 caller-owned Instance 时,父级销毁只会解除父子关系,不会替调用方销毁 child:

const preview = await runtime.create({ artifact: PreviewCard() });
await panel.mountChild({
key: 'preview',
instance: preview,
ownership: 'caller',
parentKey: null,
mount: { kind: 'root' },
});
await runtime.destroy(panel);
await runtime.destroy(preview); // 仍由调用方负责

该方法只修改当前 Runtime,不会写回 Artifact。需要保存层级时,应编译 Composition IR。

参数

specRuntimeChildSpec
optionsReconcileRuntimeChildrenOptions