// resolveDirective函数可以引入自定义指令
const elLoading = resolveDirective('loading')
return () => {
// withDirectives函数可以将自定义指令应用于vNode
// withDirectives的第一个参数为vNode
return withDirectives(h('div', {class: 'table'}, [
h(
ElTable,
{
class: 'table',
ref: 'table',
border: true,
size: 'mini',
data: state.data,
...props.props,
},
{
default: () => {
return renderColumn()
},
}
),
]), [[elLoading, state.loading]]) // withDirectives的第二个参数为resolveDirective引入的自定义指令
}
vue3中的两个函数
1、resolveDirective
如果在当前应用实例中可用,则允许通过其名称解析一个directive。
返回一个Directive,如果没有找到,则返回undefined
只能在render或setup函数中使用。
const app = Vue.createApp({})
app.directive('highlight', {})
import { resolveDirective } from 'vue'
render () {
const highlightDirective = resolveDirective('highlight')
}
2、withDirectives
允许将指令应用于VNode,返回一个包含应用指令的VNode。
withDirectives只能在render或setup函数中使用。
withDirectives(vnode,directives)
directives:[[directive, value, arg, modifiers],[...]]
value: 例如:v-my-directive="1 + 1" 中,绑定值为 2。
arg: 例如 v-my-directive:foo 中,参数为 "foo"。
modifiers: 例如:v-my-directive.foo.bar 中,修饰符对象为 { foo: true, bar: true }。
const MyDirective = resolveDirective('MyDirective')
const nodeWithDirectives = withDirectives(h('div'),[[MyDirective, 100, 'click', { prevent: true }]])