Skip to content

在组件中访问 Runtime 实例

inject(PTAH_INSTANCE)

PtahJs 会在 <DesignerRoot><Preview> 内通过 Vue 的依赖注入提供 runtime。

js
import { inject } from 'vue';
import { PTAH_INSTANCE } from '@ptahjs/ui-vue';

const app = inject(PTAH_INSTANCE);

useRuntimeApp()

用于创建预览或运行模式下的 Runtime 实例。

js
const runtime = useRuntimeApp({ id: 've1' });

useDesignerApp()

用于创建设计器模式下的 Runtime 实例,并附带 runtime.designer

js
const runtime = useDesignerApp({ id: 've1' });
const designer = runtime.designer;

访问 app.$state

vue
<script setup>
import { inject, computed } from 'vue';
import { PTAH_INSTANCE } from '@ptahjs/ui-vue';

const app = inject(PTAH_INSTANCE);
const currentScene = computed(() => app.$state.currentScene);
</script>

常用状态键

Key说明
scenesById场景映射
modulesById模块映射
eventsById事件映射
childrenIdsByParentId父子关系映射
sceneIdOrder场景顺序
currentSceneId当前场景 ID
currentModuleId当前模块 ID
currentScene当前场景
currentModule当前模块