vue3 vNode、h函数中使用自定义指令,如v-loading

本文介绍了Vue3中用于处理自定义指令的两个关键函数:resolveDirective用于根据名称获取自定义指令,而withDirectives则用于在VNode上应用这些指令。这两个函数都在渲染或setup函数内使用,例如在示例中展示了如何结合使用它们来在组件中实现加载指示器。
摘要由CSDN通过智能技术生成
// 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" 中,绑定值为 2arg:		例如 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 }]])
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值