Skip to content

Commit e12cef4

Browse files
committed
fix(vue-xrender): xTpl component throw warnning
1 parent fc899de commit e12cef4

File tree

4 files changed

+114
-4
lines changed

4 files changed

+114
-4
lines changed

packages/doc-site/.vuepress/client.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,11 @@ export default defineClientConfig({
1111
configLoadSandbox(preOptions => {
1212
return {
1313
...preOptions,
14+
pkgCdn: {
15+
'@vue/runtime-dom'(version) {
16+
return `https://unpkg.com/vue@${version}/dist/vue.esm-browser.js`
17+
}
18+
},
1419
lifeCycle: {
1520
// TODO: FIX types declaration
1621
loadTsLibs(_, defaultTsLibs) {

packages/doc-site/zh/libs/vue-xrender/components.md

Lines changed: 97 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,18 @@
22

33
## XJsx
44

5-
用于在 template 渲染 jsx 的组件。
5+
### 介绍
6+
7+
用于在 `template` 渲染 `jsx` 的组件。
8+
`jsx` function 会接收一个 `props``props` 的属性 `children` 指向 `<XJsx></XJsx>` 的默认插槽
69

710
### props
811

912
| 参数 | 说明 | 类型 | 是否必填 | 默认值 |
1013
| ---- | ---------------- | ------------------------------------------------------------------------------------------------- | -------- | ------ |
1114
| jsx | 要动态渲染的 jsx | `FunctionComponent` \|<br/><br/> `VNode` \|<br/><br/>`(props: Object, h: CreateElement) => VNode` || - |
1215

13-
### 示例
16+
### 用法
1417

1518
#### composition-api 使用
1619

@@ -122,6 +125,48 @@ export default {
122125
</script>
123126
```
124127

128+
### 示例
129+
130+
::: demo XJsx Demo
131+
132+
```vue App.vue
133+
<template>
134+
<x-jsx :jsx="CardJsx"> CardJsx 的 children 节点 </x-jsx>
135+
</template>
136+
137+
<script lang="tsx">
138+
import {defineComponent, ref} from 'vue'
139+
import {JsxFn, XJsx} from 'vue-xrender'
140+
141+
export default defineComponent({
142+
components: {
143+
XJsx
144+
},
145+
setup() {
146+
const time = ref(0)
147+
148+
setInterval(() => {
149+
time.value++
150+
}, 1000)
151+
152+
const CardJsx: JsxFn = props => (
153+
<div>
154+
<h1>CardJsx 组件,在 setup jsx 函数中创建,在模板中使用</h1>
155+
<h2>CardJsx 实时时间: {time.value}s</h2>
156+
{props.children}
157+
</div>
158+
)
159+
160+
return {
161+
CardJsx
162+
}
163+
}
164+
})
165+
</script>
166+
```
167+
168+
:::
169+
125170
### 注意
126171

127172
项目要配置 Jsx 编译器,否则无法编译 Jsx 语法
@@ -190,7 +235,13 @@ module.exports = {
190235

191236
## XTpl
192237

193-
用于渲染动态 template 字符串的组件
238+
### 介绍
239+
240+
用于渲染动态 `template` 字符串的组件
241+
242+
`template` 字符串可在运行时随时拼接变更
243+
244+
本组件不支持传递插槽
194245

195246
### props
196247

@@ -199,7 +250,7 @@ module.exports = {
199250
| tpl | 要动态编译的 template 字符串 | string || - |
200251
| ctx | template 字符串的上下文 | Object || XTpl.$parent \|\| {} |
201252

202-
### 示例
253+
### 用法
203254

204255
#### composition-api 使用
205256

@@ -309,6 +360,48 @@ export default {
309360
</script>
310361
```
311362

363+
### 示例
364+
365+
::: demo XTpl Demo
366+
367+
```vue App.vue
368+
<template>
369+
<x-tpl :tpl="CardTpl" />
370+
</template>
371+
372+
<script lang="ts">
373+
import {defineComponent, ref} from 'vue'
374+
import {XTpl} from 'vue-xrender'
375+
376+
export default defineComponent({
377+
components: {
378+
XTpl
379+
},
380+
setup() {
381+
const time = ref(0)
382+
383+
setInterval(() => {
384+
time.value++
385+
}, 1000)
386+
387+
const CardTpl = `
388+
<div>
389+
<h1>CardTpl 动态模板字符串,在模板中使用</h1>
390+
<h2>CardTpl 实时时间: {{time}}s</h2>
391+
</div>
392+
`
393+
394+
return {
395+
time, // 注意要在此处暴露 time,否则 vm 实例上会找不到 time 渲染
396+
CardTpl
397+
}
398+
}
399+
})
400+
</script>
401+
```
402+
403+
:::
404+
312405
### 注意
313406

314407
**该组件依赖于 vue 运行时 compiler**,请在打包器配置将别名(alias)指向 **vue 完整版**

packages/vue-xrender/src/components/x-tpl/index.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,14 @@ const vm = defineComponent({
6868
if (isVue2) {
6969
finalCtx.$slots = $slots
7070
} else {
71+
if (finalCtx._) {
72+
// $parent is vm, not setup sugar expose
73+
const _vm = finalCtx._
74+
finalCtx = {
75+
...vm.data,
76+
..._vm.setupState
77+
}
78+
}
7179
// vue3 cover $slots will throw error
7280
finalCtx = {
7381
...finalCtx,

packages/vuepress-plugin-sandbox/src/clientConfigFile.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,10 @@ export default defineClientConfig({
1717
// set global css
1818
const styleEl = document.createElement('style')
1919
styleEl.innerHTML = `
20+
.demo-container {
21+
margin-top: 1rem;
22+
margin-bottom: 1rem;
23+
}
2024
.demo-container + .demo-container {
2125
margin-top: 3rem;
2226
}

0 commit comments

Comments
 (0)