From e12cef403121301cf57fcbd74d21e08519af8e95 Mon Sep 17 00:00:00 2001 From: yangjinming <2214962083@qq.com> Date: Thu, 19 May 2022 14:57:23 +0800 Subject: [PATCH] fix(vue-xrender): xTpl component throw warnning --- packages/doc-site/.vuepress/client.ts | 5 + .../zh/libs/vue-xrender/components.md | 101 +++++++++++++++++- .../vue-xrender/src/components/x-tpl/index.ts | 8 ++ .../src/clientConfigFile.ts | 4 + 4 files changed, 114 insertions(+), 4 deletions(-) diff --git a/packages/doc-site/.vuepress/client.ts b/packages/doc-site/.vuepress/client.ts index 200c14d..091a6db 100644 --- a/packages/doc-site/.vuepress/client.ts +++ b/packages/doc-site/.vuepress/client.ts @@ -11,6 +11,11 @@ export default defineClientConfig({ configLoadSandbox(preOptions => { return { ...preOptions, + pkgCdn: { + '@vue/runtime-dom'(version) { + return `https://unpkg.com/vue@${version}/dist/vue.esm-browser.js` + } + }, lifeCycle: { // TODO: FIX types declaration loadTsLibs(_, defaultTsLibs) { diff --git a/packages/doc-site/zh/libs/vue-xrender/components.md b/packages/doc-site/zh/libs/vue-xrender/components.md index 34fff5c..6f8cdd2 100644 --- a/packages/doc-site/zh/libs/vue-xrender/components.md +++ b/packages/doc-site/zh/libs/vue-xrender/components.md @@ -2,7 +2,10 @@ ## XJsx -用于在 template 渲染 jsx 的组件。 +### 介绍 + +用于在 `template` 渲染 `jsx` 的组件。 +`jsx` function 会接收一个 `props`,`props` 的属性 `children` 指向 `` 的默认插槽 ### props @@ -10,7 +13,7 @@ | ---- | ---------------- | ------------------------------------------------------------------------------------------------- | -------- | ------ | | jsx | 要动态渲染的 jsx | `FunctionComponent` \|

`VNode` \|

`(props: Object, h: CreateElement) => VNode` | 是 | - | -### 示例 +### 用法 #### composition-api 使用 @@ -122,6 +125,48 @@ export default { ``` +### 示例 + +::: demo XJsx Demo + +```vue App.vue + + + +``` + +::: + ### 注意 项目要配置 Jsx 编译器,否则无法编译 Jsx 语法 @@ -190,7 +235,13 @@ module.exports = { ## XTpl -用于渲染动态 template 字符串的组件 +### 介绍 + +用于渲染动态 `template` 字符串的组件 + +`template` 字符串可在运行时随时拼接变更 + +本组件不支持传递插槽 ### props @@ -199,7 +250,7 @@ module.exports = { | tpl | 要动态编译的 template 字符串 | string | 是 | - | | ctx | template 字符串的上下文 | Object | 否 | XTpl.$parent \|\| {} | -### 示例 +### 用法 #### composition-api 使用 @@ -309,6 +360,48 @@ export default { ``` +### 示例 + +::: demo XTpl Demo + +```vue App.vue + + + +``` + +::: + ### 注意 **该组件依赖于 vue 运行时 compiler**,请在打包器配置将别名(alias)指向 **vue 完整版** diff --git a/packages/vue-xrender/src/components/x-tpl/index.ts b/packages/vue-xrender/src/components/x-tpl/index.ts index 114f191..cf54ddf 100644 --- a/packages/vue-xrender/src/components/x-tpl/index.ts +++ b/packages/vue-xrender/src/components/x-tpl/index.ts @@ -68,6 +68,14 @@ const vm = defineComponent({ if (isVue2) { finalCtx.$slots = $slots } else { + if (finalCtx._) { + // $parent is vm, not setup sugar expose + const _vm = finalCtx._ + finalCtx = { + ...vm.data, + ..._vm.setupState + } + } // vue3 cover $slots will throw error finalCtx = { ...finalCtx, diff --git a/packages/vuepress-plugin-sandbox/src/clientConfigFile.ts b/packages/vuepress-plugin-sandbox/src/clientConfigFile.ts index e21320d..36cc5b9 100644 --- a/packages/vuepress-plugin-sandbox/src/clientConfigFile.ts +++ b/packages/vuepress-plugin-sandbox/src/clientConfigFile.ts @@ -17,6 +17,10 @@ export default defineClientConfig({ // set global css const styleEl = document.createElement('style') styleEl.innerHTML = ` + .demo-container { + margin-top: 1rem; + margin-bottom: 1rem; + } .demo-container + .demo-container { margin-top: 3rem; }