javascript - 为什么 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 中做错了什么?
对不起,我一定在其他地方犯了错误,因为这适用于小提琴。
解决方案
Vue 状态管理的存在有一系列原因,使状态变量反应性就是其中之一。我建议阅读/观看官方文档上的解释
如果您想实现自己的香草商店,您可以使用Vue.set
或更新您的商店this.$set
。
推荐阅读
- python - 如何从给定的起点和终点返回列表
- flutter - 增加 Flutter 中自定义缓存管理器的 validTill 时间
- elasticsearch - 关键词查询
- firebase - 状态不佳:无法在不存在 FirebaseFirestore 的 DocumentSnapshotPlatform 上获取字段
- apt - Aptly 命令无法打开数据库
- deep-learning - 选择多少对来计算 ROC 曲线?
- javascript - 状态对象被多次调用如何删除最旧的对象
- node.js - 错误:列出用户谷歌日历事件时未设置访问、刷新令牌或 API 密钥
- google-sheets - 无法使用工作表在 2 个日期之间获取查询公式结果
- selenium - 点击按钮后,语句不会向下执行,也不会报超时