Vue之監(jiān)聽數(shù)據(jù)的原理詳解
更新時間:2022年01月10日 08:57:24 作者:王同學(xué)要努力
這篇文章主要為大家介紹了Vue之監(jiān)聽數(shù)據(jù)的原理,具有一定的參考價值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來幫助<BR>

<body>
<div id="root">
<h1>學(xué)生的基本信息</h1>
<button @click="student.age++">年齡+1歲</button>
<button @click="addSex">添加性別屬性默認(rèn)值是男</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>
<h3>姓名:{{student.name}}</h3>
<h3>年齡:{{student.age}}</h3>
<h3 v-if="student.sex">性別:{{student.sex}}</h3>
<h3>愛好:</h3>
<hr>
<ul>
<li v-for="(h,index) in student.hobby" :key="index">{{h}}</li>
</ul>
<hr>
<h3>朋友們:</h3>
<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個開始,刪除一個,新增加的值是爬山
//注意:不能直接通過數(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>
總結(jié)
本篇文章就到這里了,希望能夠給你帶來幫助,也希望您能夠多多關(guān)注腳本之家的更多內(nèi)容!
相關(guān)文章
vue2從數(shù)據(jù)變化到視圖變化之nextTick使用詳解
這篇文章主要為大家介紹了vue2從數(shù)據(jù)變化到視圖變化之nextTick使用詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-09-09
使用VUE實現(xiàn)在table中文字信息超過5個隱藏鼠標(biāo)移到時彈窗顯示全部
這篇文章主要介紹了使用VUE實現(xiàn)在table中文字信息超過5個隱藏,鼠標(biāo)移到時彈窗顯示全部,本文通過實例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價值,需要的朋友可以參考下2019-09-09
vue3通過ref獲取子組件defineExpose的數(shù)據(jù)和方法
defineExpose是Vue3中新增的選項,用于向父組件暴露子組件內(nèi)部的屬性和方法,通過defineExpose,子組件可以主動控制哪些屬性和方法可以被父組件訪問,本文主要介紹了vue3通過ref獲取子組件defineExpose的數(shù)據(jù)和方法,需要的朋友可以參考下2023-10-10

