对于监听修改模块导致在控制台打印出undefined
== 和 ===的规则
watch: {
// 监听路径变动
path(value) {
console.log(value);
this.ruleForm.parentId = value[value.length - 1];
},
}
// 树形结点获取路径
const getSelectedKeys = function (id, tree, keys = [], temps = []) {
// 遍历树形数据
for (let node of tree) {
// 把树形数据转换为水平的数组数据
temps[node.depth] = node.id;
if (node.id === id) { // 将其对应的路径放入keys中
keys.push(temps.slice(0, node.depth + 1));
break;
}
if (node.children) {
getSelectedKeys(id, node.children, keys, temps);
}
}
return keys;
}
在 visible 监听中去向后端请求修改的数据时:
// 查询到需要修改的数据
this.$axios.get(`/categories/${this.data.id}`).then(response => {
this.ruleForm = response.data.data;
console.log(this.ruleForm);
// 将树形结构转换为水平的数组
let keys = getSelectedKeys(this.ruleForm.parentId, this.categories);
this.path = ['0'].concat(keys[0]); // 拼接路径 - ['0']是根节点
})
在函数中传递两种数据:一种是 data.pid ,另一种是r uleform.parentId。
当注释掉拼接路径之后,pid 出现,而且和 data.pid 的不同,是 number 类型
undefined 时打印的监听的 path 中的 value 值:
parentId 确实传递到 get** 函数中,不过被监听修改成为了 undefined,控制台打印存在延迟。
- 该部分代码中,ruleForm 中的 parentId 为 undefined,但是是可以获取到值的,当不执行 拼接 时,就发现存在 parentId。(监听没有被修改)
- 修改部分 this.selectedCategoryIds 后的赋值句时,例如 = keys后,parentId 为一个 Array。
最后在这里用字符串的方式进行判断,可以正确打印出结果:打印 id 和 node.id 的数据类型后:
可以修改为==
强制比较下: