javascript - Vue.js 未使用 push() 检测数据更改
问题描述
attributes
我有这个方法可以在我的对象中设置数据结构。
setAttributes(data) {
const attr = data.attributes;
attr.forEach(attribute => {
attribute.attributes.forEach(item => {
if (!this.attributes[attribute.name]) {
this.$set(this.attributes, attribute.name, {
name: attribute.name,
attributes: []
});
}
if (!this.attributes[attribute.name].attributes[item.id]) {
this.$set(this.attributes[attribute.name].attributes, item.id, {
name: item.name.value,
attributes: []
});
}
this.attributes[attribute.name].attributes[item.id].attributes.push(item);
});
});
}
一切正常,除了最后一行this.attributes[attribute.name].attributes[item.id].attributes.push(item);
Vue 没有检测到数据变化并且数组仍然是空的。
据我所知,push()
应该让 Vue 检测到数据更改还是不正确?
解决方案
得到它使用此代码:
setAttributes(data) {
const attr = data.attributes;
attr.forEach(attribute => {
const name = attribute.name
.toLowerCase()
.replace(/[^a-z0-9 -]/g, '')
.replace(/\s+/g, '_');
if (!this.attributes[name]) {
this.$set(this.attributes, name, {
name: attribute.name,
attributes: {}
});
}
attribute.attributes.forEach(item => {
if (!this.attributes[name].attributes[item.id]) {
this.$set(this.attributes[name].attributes, item.id, {
name: item.name.value,
attributes: []
});
}
this.attributes[name].attributes[item.id].attributes.push(item);
});
});
}
问题是attributes
在第一个 if 语句中设置为数组而不是对象。
推荐阅读
- c++ - CreateThread 以错误的输出结束
- excel - 接下来没有编译错误(但我都有..)
- sql - 自加入获取报表的第一个 classID
- scala - SBT:如何将上层目录中的 sbt 文件包含到我的 build.sbt 中?
- html - DataTable - 列表的第一个单选按钮从不显示已选中
- html - 如何将背景图像添加到 div 列中
- excel - 使用 SpecialCells(x1CellTypeVisible) 清除多个过滤列
- r - 通过其在 R 中的值对多列进行子集
- servicestack - 从 ServiceStack 请求中获取 API Key
- java - 错误 net.sourceforge.tess4j.Tesseract - null ,同时执行 doOCR 功能