docs: 修改onRuntimeReady描述

This commit is contained in:
roymondchen 2022-07-29 16:56:12 +08:00 committed by jia000
parent 05f9bb8141
commit 8e1e7ef0de

View File

@ -149,12 +149,23 @@ devServer: {
这是因为在runtime中无法直接获取到editor中的dsl所以需要通过editor注入到window的magic api来交互 这是因为在runtime中无法直接获取到editor中的dsl所以需要通过editor注入到window的magic api来交互
在App.vue中通过监听message来准备获取magic注入时机然后调用magic.onRuntimeReady示例代码如下 > 如出现在runtime中出现magic为undefined 可以尝试在App.vue中通过监听message来准备获取magic注入时机然后调用magic.onRuntimeReady示例代码如下
```js
window.addEventListener('message', ({ data }) => {
if (!data.tmagicRuntimeReady) {
return;
}
window.magic?.onRuntimeReady({
// ...
});
});
```
> 这里可能会出现editor抛出message的时候runtime还没有执行到监听message的情况 > 这里可能会出现editor抛出message的时候runtime还没有执行到监听message的情况
> 编辑器只在iframe onload事件中抛出message > 编辑器只在iframe onload事件中抛出message
> 如果出现runtime中接收不到message的情况可以尝试在onMounted的时候调用magic.onRuntimeReady > 如果出现runtime中接收不到message的情况可以尝试在onMounted的时候调用magic.onRuntimeReady
```ts ```ts
import type { Magic } from '@tmagic/stage'; import type { Magic } from '@tmagic/stage';
@ -165,19 +176,13 @@ declare global {
} }
``` ```
```ts ```ts
import type { RemoveData, UpdateData } from '@tmagic/stage'; import type { RemoveData, UpdateData } from '@tmagic/stage';
import type { Id, MApp, MNode } from '@tmagic/schema'; import type { Id, MApp, MNode } from '@tmagic/schema';
const root = ref<MApp>(); const root = ref<MApp>();
window.addEventListener('message', ({ data }) => { window.magic?.onRuntimeReady({
if (!data.tmagicRuntimeReady) {
return;
}
window.magic?.onRuntimeReady({
/** 当编辑器的dsl对象变化时会调用 */ /** 当编辑器的dsl对象变化时会调用 */
updateRootConfig(config: MApp) { updateRootConfig(config: MApp) {
root.value = config; root.value = config;
@ -205,7 +210,6 @@ window.addEventListener('message', ({ data }) => {
const index = page.value.items?.findIndex((child: MNode) => child.id === id); const index = page.value.items?.findIndex((child: MNode) => child.id === id);
page.value.items.splice(index, 1); page.value.items.splice(index, 1);
}, },
});
}); });
``` ```