您好,登錄后才能下訂單哦!
這篇文章將為大家詳細(xì)講解有關(guān)Vue中監(jiān)聽數(shù)據(jù)的原理是什么,文章內(nèi)容質(zhì)量較高,因此小編分享給大家做個(gè)參考,希望大家閱讀完這篇文章后對相關(guān)知識(shí)有一定的了解。
<body> <div id="root"> <h2>學(xué)生的基本信息</h2> <button @click="student.age++">年齡+1歲</button> <button @click="addSex">添加性別屬性默認(rèn)值是男</button><br> <button @click="student.sex='未知' ">修改屬性值</button><br> <button @click="addFriend">在列表的首位就添加一個(gè)朋友</button><br> <button @click="updateFriend">更新第一個(gè)人的名字</button><br> <button @click="addHobby">添加一個(gè)愛好</button><br> <button @click="change">修改第一個(gè)愛好為爬山</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個(gè)開始,刪除一個(gè),新增加的值是爬山 //注意:不能直接通過數(shù)組下標(biāo)的形式進(jìn)行修改 //this.student.hobby.splice(0, 1, '爬山') //Vue.set(this.student.hobby, 0, '爬山') this.$set(this.student.hobby, 0, '爬山') }, removeSmoke() { //filter不影響原數(shù)組的改變 this.student.hobby = this.student.hobby.filter((h) => { return h !== '抽煙' }) } } }) </script> </body>
關(guān)于Vue中監(jiān)聽數(shù)據(jù)的原理是什么就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,可以學(xué)到更多知識(shí)。如果覺得文章不錯(cuò),可以把它分享出去讓更多的人看到。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。