vue3 watch新旧值一样
watch(
() => route.path,
(path) => {
if (!path.includes('view-details')) {
addTags();
moveToCurrentTag();
}
}
);
path 没有变化 watch里面不会执行
不生效
watch(
() => JSON.parse(JSON.stringify(route.path)),
(path) => {
if (!path.includes('view-details')) {
addTags();
moveToCurrentTag();
}
}
);
const newPath = computed(() => {
return JSON.stringify(route.path);
});
watch(
() => newPath.value,
(path) => {
console.log('sss')
if (!path.includes('view-details')) {
addTags();
moveToCurrentTag();
}
}
);
const newPath = computed(() => {
return JSON.stringify(route.path);
});
watch(
newPath,
(path,oldPath) => {
console.log('oldPath',oldPath)
console.log('path',path)
if (!path.includes('view-details')) {
addTags();
moveToCurrentTag();
}
},
{
immediate:true
}
);
解决
const newRoute = computed(() => {
return JSON.stringify(route);
});
watch(
newRoute,
(newVal) => {
const newValPath = JSON.parse(newVal) ? JSON.parse(newVal).path : '';
if (newValPath && !newValPath.includes('view-details')) {
addTags();
moveToCurrentTag();
}
},
{ deep: true }
);
或者是 { ....结构 }
watch(() => {
return {
...vectorLayerMap
}
}, (newValue, oldValue) => {
console.log('newValue, oldValue 数据发生了变化', newValue, oldValue)
}, {
deep: true
})
参考:
vue2写法: blog.csdn.net/qq_41417772…