h() 是 hyperscript 的简称——意思是“能生成 HTML (超文本标记语言) 的 JavaScript”
attribute:html特性
property:dom属性
attribute 和 property 都能在 prop 中书写,Vue 会自动将它们分配到正确的位置
attribute 和 property 简写法:可以分别通过
(html特性)和 ^
(dom属性)前缀来添加.
没有 props 时可以省略不写
function render() {
const p = h('p', 'hi')
return h('div', [
// error 重复的 vnodes 是无效的
p,
p
])
}
.passive
、 .capture
和 .once
事件修饰符h('input', {
onClickCapture() {
/* 捕捉模式中的监听器 */
},
onKeyupOnce() {
/* 只触发一次 */
},
onMouseoverOnceCapture() {
/* 单次 + 捕捉 */
}
})
import { withModifiers } from 'vue'
h('div', {
onClick: withModifiers(() => {}, ['self'])
})
export default {
props: ['message'],
setup(props, { slots }) {
return () => [
h('div', slots.default()),
h(
'div',
slots.footer({
text: props.message
})
)
]
}
}
h(MyComponent, null, {
default: () => 'default slot',
foo: () => h('div', 'foo')
})
v-model
export default {
emits: ['update:modelValue'],
props: ['modelValue'],
setup(props, { emit }) {
return () =>
h(SomeComponent, {
modelValue: props.modelValue,
'onUpdate:modelValue': (value) => emit('update:modelValue', value)
})
}
}
import { h, withDirectives } from 'vue'
// 自定义指令
const pin = {
mounted() { /* ... */ },
updated() { /* ... */ }
}
// <div v-pin:top.animate="200"></div>
const vnode = withDirectives(h('div'), [
[pin, 200, 'top', { animate: true }]
])