gpt4 book ai didi

vue.js - 如何在自定义 VueJS 指令中声明变量?

转载 作者:搜寻专家 更新时间:2023-10-30 22:32:21 46 4
gpt4 key购买 nike

当我尝试像这样以通常的方式声明变量时出现错误:

   Vue.directive('my-directive', {
varA: '',
inserted: function(el, binding, vnode){
this.varA = 'test'; // TypeError: Cannot set property
// 'varA ' of undefined
},
});

TypeError: Cannot set property 'varA ' of undefined

如何在 Vue 指令中声明变量?

最佳答案

在 Vue 指令中,当您需要存储一个值时,它通常存储在元素中。

Vue.directive('my-directive', {
inserted: function(el, binding, vnode){
el.varA = 'test';
},
});

该元素作为参数传入指令中的每个生命周期事件,您稍后可以从 el.varA 访问该值。

关于vue.js - 如何在自定义 VueJS 指令中声明变量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45050066/

46 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com