首页 > 解决方案 > 为什么 Vue 数据在他的情况下没有更新

问题描述

我正在努力完全理解 Vue。说我有:

<!--store.js-->
export default {
  x:{
    p:{},
  },
}
<!--text.vue-->
<template>
  <div class="test-comp">Test component</div>
</template>

<script>
import store from '../store';
export default {
   data(){
     return {
       p:store.x.p,
     };
   },
   methods:{
      loadData(){
        store.x.p.a='some data';
      },
   },
   mounted(){
     this.loadData();
   }
}
</script>

在上面 this.p有一个值{}即空对象,运行文件后。但是,在没有 Vue 的 Javascript 中: let p=store.x.p是一个引用,因此p引用的值与store.x.p

我在 Vue 中做错了什么?

对不起,我一定在其他地方犯了错误,因为这适用于小提琴。

标签: javascriptvue.js

解决方案


Vue 状态管理的存在有一系列原因,使状态变量反应性就是其中之一。我建议阅读/观看官方文档上的解释

如果您想实现自己的香草商店,您可以使用Vue.set或更新您的商店this.$set


推荐阅读