这篇文章将为大家详细讲解有关Vue中监听数据的原理是什么,文章内容质量较高,因此小编分享给大家做个参考,希望大家阅读完这篇文章后对相关知识有一定的了解。<body> <div&
这篇文章将为大家详细讲解有关Vue中监听数据的原理是什么,文章内容质量较高,因此小编分享给大家做个参考,希望大家阅读完这篇文章后对相关知识有一定的了解。
<body> <div id="root"> <h2>学生的基本信息</h2> <button @click="student.age++">年龄+1岁</button> <button @click="addSex">添加性别属性默认值是男</button><br> <button @click="student.sex='未知' ">修改属性值</button><br> <button @click="addFriend">在列表的首位就添加一个朋友</button><br> <button @click="updateFriend">更新第一个人的名字</button><br> <button @click="addHobby">添加一个爱好</button><br> <button @click="change">修改第一个爱好为爬山</button><br> <button @click="removeSmoke">过滤掉抽烟</button><br> <h4>姓名:{{student.name}}</h4> <h4>年龄:{{student.age}}</h4> <h4 v-if="student.sex">性别:{{student.sex}}</h4> <h4>爱好:</h4> <hr> <ul> <li v-for="(h,index) in student.hobby" :key="index">{{h}}</li> </ul> <hr> <h4>朋友们:</h4> <ul> <li v-for="(f,index) in student.friends" :key="index">{{f.name}}--{{f.age}}</li> </ul> </div> <script> Vue.config.productionTip = false; const vm = new Vue({ el: "#root ", data: { student: { name: 'zhang', age: 18, hobby: ['喝酒', '抽烟', '烫头'], friends: [{ name: 'li', age: 15 }, { name: 'wang', age: 10 }] } }, methods: { addSex() { this.$set(this.student, 'sex', '男') // Vue.set(vm.student, 'sex', '男') }, addFriend() { this.student.friends.unshift({ name: 'YY', age: 66 }) }, updateFriend() { this.student.friends[0].name = "小刘"; this.student.friends[0].age = 22 }, addHobby() { this.student.hobby.push('唱歌') }, change() { //splice添加表示从第0个开始,删除一个,新增加的值是爬山 //注意:不能直接通过数组下标的形式进行修改 //this.student.hobby.splice(0, 1, '爬山') //Vue.set(this.student.hobby, 0, '爬山') this.$set(this.student.hobby, 0, '爬山') }, removeSmoke() { //filter不影响原数组的改变 this.student.hobby = this.student.hobby.filter((h) => { return h !== '抽烟' }) } } }) </script></body>
关于Vue中监听数据的原理是什么就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。
--结束END--
本文标题: Vue中监听数据的原理是什么
本文链接: https://www.lsjlt.com/news/306083.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
下载Word文档到电脑,方便收藏和打印~
2024-05-24
2024-05-24
2024-05-24
2024-05-24
2024-05-24
2024-05-24
2024-05-24
2024-05-24
2024-05-24
2024-05-24
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0