这是一个简单的问题:“我可以将json响应映射到类而不用vue.set或通过将参数传递给类并直接分配它们来专门枚举字段吗?”在我看来这是可能的。
在React中,我可以将数据映射到类。
-谢谢
所以:
a)我可以在vue / vuex中执行地图吗? (假设语法正确吗?)
b)我是否坚持使用vue.set或手动分配?
c)是否有比Vue-Model更好的软件包?
谢谢
返回的数据:
{
abc: "Some Text",
def: 9999.999,
ghi: false
}
所需字段集:
function anItem(payload){
this.abc = "";
this.def = 0;
this.ghi = false;
this.notices = [];
this.selected = false;
this.is_updating = false;
}
变种人
const mutations = {
setStuff (state, payload) {
/*this.state.myList.items = payload;*/ <- this works, of course.
this.state.mylist.items = payload.map(a => new anItem(a)) <-- syntax here?
},
};
最佳答案
构造对象时,可以将参数用作初始值:
function anItem(payload){
this.abc = payload.abc; // changed this line
this.def = payload.def; // changed this line
this.ghi = payload.ghi; // changed this line
this.notices = [];
this.selected = false;
this.is_updating = false;
}
然后,更改增幅器以传递此类参数:
const mutations = {
setStuff (state, payload) {
this.state.mylist.items = new anItem(payload);
},
};
关于javascript - Vue.js在没有vue的情况下将JSON动态映射到Class,而无需在Vuex存储中设置或直接分配,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/49118262/