主题
渲染函数
渲染函数参数
2.x
在 2.x 中,render
函数会自动接收 h
函数 (它是 createElement
的惯用别名) 作为参数:
js
export default {
render(h) {
return h('div')
},
}
3.x
在 3.x 中,h
函数现在是全局导入的,而不是作为参数自动传递。
js
import { h } from 'vue'
export default {
render() {
return h('div')
},
}
VNode Prop 格式化
2.x
在 2.x 中,domProps
包含 VNode prop 中的嵌套列表:
js
{
staticClass: 'button',
class: { 'is-outlined': isOutlined },
staticStyle: { color: '#34495E' },
style: { backgroundColor: buttonColor },
attrs: { id: 'submit' },
domProps: { innerHTML: '' },
on: { click: submitForm },
key: 'submit-button'
}
3.x
在 3.x 中,整个 VNode prop 的结构都是扁平的。
js
{
class: ['button', { 'is-outlined': isOutlined }],
style: [{ color: '#34495E' }, { backgroundColor: buttonColor }],
id: 'submit',
innerHTML: '',
onClick: submitForm,
key: 'submit-button'
}
注册组件
2.x
在 2.x 中,注册一个组件后,把组件名作为字符串传递给渲染函数的第一个参数,它可以正常地工作:
js
Vue.component('button-counter', {
data() {
return {
count: 0,
}
},
template: `
<button @click="count++">
Clicked {{ count }} times.
</button>
`,
})
export default {
render(h) {
return h('button-counter')
},
}
3.x
在 3.x 中,由于 VNode 是上下文无关的,不能再用字符串 ID 隐式查找已注册组件。取而代之的是,需要使用一个导入的 resolveComponent
方法:
js
import { h, resolveComponent } from 'vue'
export default {
setup() {
const ButtonCounter = resolveComponent('button-counter')
return () => h(ButtonCounter)
},
}