在我们使用vue进行开发的过程中,可能会遇到一种情况:当生成vue实例后,当再次给数据赋值时,有时候并不会自动更新到视图上去;
当我们去看vue文档的时候,会发现有这么一句话:如果在实例创建之后添加新的属性到实例上,它不会触发视图更新。如下代码:
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>vue $set</title> <script src="https://static.runoob.com/assets/vue/1.0.11/vue.min.js"></script> </head> <body> <div id="app"> 姓名:{{ name }}<br> 年龄:{{age}}<br> 性别:{{sex}}<br> 说明:{{info.content}} </div> <!-- JavaScript 代码需要放在尾部(指定的HTML元素之后) --> <script> var data = { name: "简书", age: ‘3‘, info: { content: ‘my name is test‘ } } var key = ‘content‘; var vm = new Vue({ el:‘#app‘, data: data, ready: function(){ //Vue.set(data,‘sex‘, ‘男‘) //this.$set(‘info.‘+key, ‘what is this?‘); } }); data.sex = ‘男‘; </script> </body> </html>
运行结果:
姓名:简书 年龄:3 性别: 说明:my name is test
为什么会这样呢?当去查对应文档时,你会发现响应系统 ,把一个普通 Javascript 对象传给 Vue 实例来作为它的 data 选项,Vue 将遍历它的属性,用 Object.defineProperty 将它们转为 getter/setter,如上示例,将data在控制台打印出来会发现:
在age及name都有get和set方法,但是在sex里面并没有这两个方法,因此,设置了sex值后vue并不会自动更新视图;
解决方法:
<script> var data = { name: "简书", age: ‘3‘, info: { content: ‘my name is test‘ } } var key = ‘content‘; new Vue({ el:‘#app‘, data: data, ready: function(){ Vue.set(data,‘sex‘, ‘男‘); this.$set(‘info.‘+key, ‘what is this?‘); } }); </script>
1、通过Vue.set方法设置data属性,如上:
Vue.set(data,‘sex‘, ‘男‘)
2、您还可以使用 vm.$set实例方法,这也是全局 Vue.set方法的别名:
var key = ‘content‘; //这种主要用于当对象中某个属性值动态生成时处理方式 this.$set(‘info.‘+key, ‘what is this?‘); 或 this.$set(‘info.content‘, ‘what is this?‘);
http://market.szonline.net/amaz/10428.html
http://market.szonline.net/amaz/10427.html
http://market.szonline.net/amaz/10426.html
http://market.szonline.net/amaz/10425.html
http://market.szonline.net/amaz/10424.html
http://market.szonline.net/amaz/10423.html
http://market.szonline.net/amaz/10422.html
http://market.szonline.net/amaz/10421.html
http://market.szonline.net/amaz/10420.html
http://market.szonline.net/amaz/10419.html
http://market.szonline.net/amaz/10418.html
http://market.szonline.net/amaz/10417.html
http://market.szonline.net/amaz/10415.html
http://market.szonline.net/amaz/10414.html
http://market.szonline.net/amaz/10413.html
http://market.szonline.net/amaz/10412.html
http://market.szonline.net/amaz/10411.html
http://market.szonline.net/amaz/10410.html
http://market.szonline.net/amaz/10409.html
http://market.szonline.net/amaz/10408.html
http://market.szonline.net/amaz/10407.html
http://market.szonline.net/amaz/10406.html
http://market.szonline.net/amaz/10405.html
http://market.szonline.net/amaz/10343.html
http://market.szonline.net/amaz/10342.html
http://market.szonline.net/amaz/10341.html
http://market.szonline.net/amaz/10340.html
http://market.szonline.net/amaz/10339.html
http://market.szonline.net/amaz/10338.html
http://market.szonline.net/amaz/10337.html
http://market.szonline.net/amaz/10336.html
http://market.szonline.net/amaz/10335.html
http://market.szonline.net/amaz/10334.html
http://market.szonline.net/amaz/10333.html
http://market.szonline.net/amaz/10332.html
http://market.szonline.net/amaz/10331.html
http://market.szonline.net/amaz/10330.html
http://market.szonline.net/amaz/10329.html
http://market.szonline.net/amaz/10328.html
http://market.szonline.net/amaz/10327.html
http://market.szonline.net/amaz/10326.html
http://market.szonline.net/amaz/10325.html
http://market.szonline.net/amaz/10324.html
http://market.szonline.net/amaz/10323.html
http://market.szonline.net/amaz/10322.html
http://market.szonline.net/amaz/10321.html
http://market.szonline.net/amaz/10320.html
http://market.szonline.net/amaz/10319.html
http://market.szonline.net/amaz/10318.html
http://market.szonline.net/amaz/10317.html
http://market.szonline.net/amaz/10316.html
http://market.szonline.net/amaz/10315.html
http://market.szonline.net/amaz/10314.html
http://market.szonline.net/amaz/10313.html
http://market.szonline.net/amaz/10312.html
http://market.szonline.net/amaz/10311.html
http://market.szonline.net/amaz/10310.html
http://market.szonline.net/amaz/10309.html
http://market.szonline.net/amaz/10308.html
http://market.szonline.net/amaz/10307.html
http://market.szonline.net/amaz/10306.html
http://market.szonline.net/amaz/10305.html
http://market.szonline.net/amaz/10304.html
http://market.szonline.net/amaz/10303.html
http://market.szonline.net/amaz/10302.html
http://market.szonline.net/amaz/10301.html
http://market.szonline.net/amaz/10300.html
http://market.szonline.net/amaz/10299.html
http://market.szonline.net/amaz/10298.html
http://market.szonline.net/amaz/10297.html
http://market.szonline.net/amaz/10296.html
http://market.szonline.net/amaz/10295.html
http://market.szonline.net/amaz/10294.html
http://market.szonline.net/amaz/10293.html
http://market.szonline.net/amaz/10292.html
原文:https://www.cnblogs.com/zjw2004112/p/11524422.html