我的 pinia 状态中有一个深层物体,我想在其中放置一个观察者。
export const useProductStore = defineStore("product", {
state: () => ({
attributes: {},
}),
});
当对象内部有数据时,它看起来像这样:
attributes: {
english: {
0: {
key: "key1",
value: "value1"
},
1: {
key: "key2",
value: "value2"
},
...
}
}
我试图在这个对象上放置一个观察器,但是当值发生变化时,观察器不会触发。这是我尝试在其中执行此操作的组件:
<script setup>
import { useProductStore } from "@/stores/ProductStore";
import { storeToRefs } from "pinia";
const productStore = useProductStore();
const { attributes } = storeToRefs(productStore);
watch(() => ({...attributes}), (newValue, oldValue) => {
console.log(oldValue);
console.log(newValue);
}, { deep: true });
</script>
这直接来自 pinia 文档,但是当我更改状态时,什么也没有发生。使用 vue 开发工具,我可以看到状态对象正在更改,所以我知道它是反应性的。我错过了什么?
Copyright 2014-2026 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号