Vuejs2 如何向Vue组件动态添加方法
当我从服务器获取数据时,比如Vuejs2 如何向Vue组件动态添加方法,vuejs2,vue-component,Vuejs2,Vue Component,当我从服务器获取数据时,比如 [{methodName:mothodValue},{methodName:mothodValue}] 然后我想要动态生成方法,比如 methods: { functionArray.forEach(function (item) { item.functionName:function () { item.functionValue; } }) } 这是我的代码 var componentName = Vue.componen
[{methodName:mothodValue},{methodName:mothodValue}]
然后我想要动态生成方法,比如
methods: {
functionArray.forEach(function (item) {
item.functionName:function () {
item.functionValue;
}
})
}
这是我的代码
var componentName = Vue.component(componentName, {
data: function () {
return {
value: value
}
},
template: componentTemplate,
methods:{
functionArray.forEach(function (item) {
item.functionName:function () {
item.functionValue;
}
})
}
})
旧代码是
methods:{
getValue : function(){
getValue(this.value);
}
}
这可能吗?如果在创建Vue组件之前返回服务器数据(我不确定是否可以在创建Vue组件之后添加方法),则可以创建如下所示的方法对象:
var methods = {};
functionArray.forEach(function (item) {
methods[item.functionName] = item.functionValue;
});
不能用代码填充对象文字,但可以在之后用类似这样的代码填充空对象文字
然后您可以将其放入组件中,如下所示:
var componentName = Vue.component(componentName, {
// ..
methods: methods
});
非常感谢你的回答,我已经说完了