javascript Vue 3-深度嵌套的React对象 prop 不能被观察或计算

t1qtbnec  于 2023-06-20  发布在  Java
关注(0)|答案(1)|浏览(134)

我正在使用vue-use从会话存储中检索一个深度嵌套的React式 prop 。ui更新成功,但computed和watch函数未更新。下面是我的代码:

  1. const defaultState = {
  2. myComp: [{ isSelected: true }]
  3. };
  4. const state = useSessionStorage("settings", defaultState, {
  5. mergeDefaults: true,
  6. });
  7. watch(state.value.myComp[0].isSelected, (old, newValue) => {
  8. console.log(newValue);
  9. });
  10. const misc = computed(() =>
  11. console.log(state.value.myComp[0].isSelected)
  12. );
ssm49v7z

ssm49v7z1#

我认为你需要在手表上添加deep选项:

  1. watch(state.value.myComp[0], (old, newValue) => {
  2. console.log(newValue);
  3. }, { deep: true });

更多信息请点击此处

相关问题