- 一、 v-for 中的 Ref 数组
- 二、插槽统一
- 三、attribute 强制行为
- 四、 $attrs 包含 class & style
- 五、自定义指令
- 六、与自定义元素的互操作性
- 七、Data 选项
- 八、函数式组件
- 九、全局 API
- 十、 key Attribute
- 十一、按键修饰符
- 十二、在 prop 的默认函数中访问 this
- 十三、渲染函数 API
- 十四、transition 组件过渡的 class 名更改
- 十五、Transition 作为根节点
- 十六、Transition Group 根元素
- 十七、v-model
- 十八、v-if 与 v-for 的优先级对比
- 十九、v-bind 合并行为
- 二十、VNode 生命周期事件名
- 二十一、侦听数组
文章来源于网络收集而来,版权归原创者所有,如有侵权请及时联系!
十三、渲染函数 API
此更改不会影响 <template>
用户。
以下是更改的简要总结:
h
现在是全局导入,而不是作为参数传递给渲染函数- 更改渲染函数参数,使其在有状态组件和函数组件的表现更加一致
- VNode 现在有一个扁平的 prop 结构
渲染函数参数
Vue 2.x
在 2.x 中, render
函数会自动接收 h
函数 (它是 createElement
的惯用别名) 作为参数:
// Vue 2 渲染函数示例
export default {
render(h) {
return h('div')
}
}
Vue 3.x
在 3.x 中, h
函数现在是全局导入的,而不是作为参数自动传递。
// Vue 3 渲染函数示例
import { h } from 'vue'
export default {
render() {
return h('div')
}
}
渲染函数签名更改
Vue 2.x
在 2.x 中, render
函数自动接收参数,如 h
函数。
// Vue 2 渲染函数示例
export default {
render(h) {
return h('div')
}
}
Vue 3.x
在 3.x 中,由于 render
函数不再接收任何参数,它将主要在 setup()
函数内部使用。这还有一个好处:可以访问在作用域中声明的响应式状态和函数,以及传递给 setup()
的参数。
import { h, reactive } from 'vue'
export default {
setup(props, { slots, attrs, emit }) {
const state = reactive({
count: 0
})
function increment() {
state.count++
}
// 返回渲染函数
return () =>
h(
'div',
{
onClick: increment
},
state.count
)
}
}
VNode Prop 格式化
Vue 2.x
在 2.x 中, domProps
包含 VNode prop 中的嵌套列表:
// 2.x
{
staticClass: 'button',
class: { 'is-outlined': isOutlined },
staticStyle: { color: '#34495E' },
style: { backgroundColor: buttonColor },
attrs: { id: 'submit' },
domProps: { innerHTML: '' },
on: { click: submitForm },
key: 'submit-button'
}
Vue 3.x
在 3.x 中,整个 VNode prop 的结构都是扁平的。使用上面的例子,来看看它现在的样子。
// 3.x 语法
{
class: ['button', { 'is-outlined': isOutlined }],
style: [{ color: '#34495E' }, { backgroundColor: buttonColor }],
id: 'submit',
innerHTML: '',
onClick: submitForm,
key: 'submit-button'
}
注册组件
Vue 2.x
在 2.x 中,注册一个组件后,把组件名作为字符串传递给渲染函数的第一个参数,它可以正常地工作:
// 2.x
Vue.component('button-counter', {
data() {
return {
count: 0
}
},
template: `
<button @click="count++">
Clicked {{ count }} times.
</button>
`
})
export default {
render(h) {
return h('button-counter')
}
}
Vue 3.x
在 3.x 中,由于 VNode 是上下文无关的,不能再用字符串 ID 隐式查找已注册组件。取而代之的是,需要使用一个导入的 resolveComponent
方法:
// 3.x
import { h, resolveComponent } from 'vue'
export default {
setup() {
const ButtonCounter = resolveComponent('button-counter')
return () => h(ButtonCounter)
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论