詳解vue 實(shí)例方法和數(shù)據(jù)
1.vm.$set
問題描述:
如何在不通過循環(huán)數(shù)據(jù)給list數(shù)據(jù)添加一個(gè)showMore屬性,并且在moreFun中改變這個(gè)新增屬性的值,并實(shí)現(xiàn)雙向綁定?
<template>
<div id="app">
<div class="demo">
<ul>
<template v-for="(v,index) in list">
<li>{{v.name}}</li>
<div v-show="!v.showMore">
<button @click="moreFun(index)">展示更多</button>
</div>
</template>
</ul>
</div>
</div>
</template>
<script>
export default {
name: 'app',
data() {
return {
list: [{
name: '小穎'
}, {
name: '仔仔'
}, {
name: '黑妞'
}, {
name: '土豆'
}]
}
},
methods: {
moreFun(index) {
console.log(this.list);
}
}
}
</script>
<style>
#app {
font-family: 'Avenir', Helvetica, Arial, sans-serif;
-webkit-font-smoothing: antialiased;
-moz-osx-font-smoothing: grayscale;
text-align: center;
color: #2c3e50;
margin-top: 60px;
}
</style>
一開始小穎并不知道怎么做,而且小穎覺得
<div v-show="!v.showMore">
<button @click="moreFun(index)">展示更多</button>
</div>
這段代碼肯定會(huì)報(bào)錯(cuò),然而當(dāng)小穎寫上后發(fā)現(xiàn),并沒有,后來那位帥鍋告訴我,看看vue的 vm.$set 小穎看后將moreFun方法寫為:
moreFun(index) {
this.$set(this.list[index], 'showMore', true);
console.log(this.list);
}
然后就達(dá)到小穎想要的結(jié)果啦。小穎當(dāng)時(shí)遇到的問題類似于這樣的:
<template>
<div id="app">
<div class="demo">
<ul>
<template v-for="(v,index) in list">
<li>{{v.name}}</li>
<div v-show="!v.showMore">
<button @click="moreFun(index)">展示更多</button>
</div>
</template>
</ul>
</div>
</div>
</template>
<script>
export default {
name: 'app',
data() {
return {
list: [{
name: '小穎'
}, {
name: '仔仔'
}, {
name: '黑妞'
}, {
name: '土豆'
}]
}
},
mounted: function() {
this.list.forEach(function(element, index) {
element.showMore = false;
});
},
methods: {
moreFun(index) {
this.list[index].showMore = true;
console.log(this.list);
}
}
}
</script>
<style>
#app {
font-family: 'Avenir', Helvetica, Arial, sans-serif;
-webkit-font-smoothing: antialiased;
-moz-osx-font-smoothing: grayscale;
text-align: center;
color: #2c3e50;
margin-top: 60px;
}
</style>
問題:當(dāng)執(zhí)行完moreFun方法后,雖然list中的showMore屬性的值變成了true,但是
<div v-show="!v.showMore"> <button @click="moreFun(index)">展示更多</button> </div>
按鈕 展示更多 仍然顯示著,這是因?yàn)?,如果在?shí)例創(chuàng)建之后添加新的屬性到實(shí)例上,它不會(huì)觸發(fā)視圖更新。
所以后來小穎就將showMore直接添加到list中,然后就好啦?,F(xiàn)在想想其實(shí)用個(gè)vm.$set就解決啦。
2.vm.$watch
用法:
觀察 Vue 實(shí)例變化的一個(gè)表達(dá)式或計(jì)算屬性函數(shù)?;卣{(diào)函數(shù)得到的參數(shù)為新值和舊值。表達(dá)式只接受監(jiān)督的鍵路徑。對(duì)于更復(fù)雜的表達(dá)式,用一個(gè)函數(shù)取代。
注意:在變異 (不是替換) 對(duì)象或數(shù)組時(shí),舊值將與新值相同,因?yàn)樗鼈兊囊弥赶蛲粋€(gè)對(duì)象/數(shù)組。Vue 不會(huì)保留變異之前值的副本。
<template>
<div id="app">
<div class="demo">
<input type="text" class="num1" v-model="num1">
<label class="sign">-</label>
<input type="text" class="num2" v-model="num2">
<label class="sign">=</label>
<label class="result">{{resultNum}}</label>
</div>
</div>
</template>
<script>
export default {
name: 'app',
data() {
return {
num1: 1,
num2: 5,
resultNum: null
}
},
watch: {
num1: function() {
var _num1 = parseInt(this.num1);
var _num2 = parseInt(this.num2);
this.resultNum = _num1 - _num2;
},
num2: function() {
var _num1 = parseInt(this.num1);
var _num2 = parseInt(this.num2);
this.resultNum = _num1 - _num2;
}
},
mounted: function() {
var _num1 = parseInt(this.num1);
var _num2 = parseInt(this.num2);
this.resultNum = _num1 - _num2;
}
}
</script>
<style>
#app {
font-family: 'Avenir', Helvetica, Arial, sans-serif;
-webkit-font-smoothing: antialiased;
-moz-osx-font-smoothing: grayscale;
text-align: center;
color: #2c3e50;
margin-top: 60px;
}
input.num1,
input.num2 {
width: 100px;
}
label.sign {
font-size: 30px;
vertical-align: -3px;
}
label.result {
font-size: 20px;
}
</style>
3.vm.$delete
用法:
這是全局 Vue.delete 的別名。
<template>
<div id="app">
<div class="demo">
<ul>
<template v-for="(v,index) in list">
<li>{{v.name}}</li>
<li>{{v.age}}</li>
<button @click="deleteFun(index)">delete</button>
</template>
</ul>
</div>
</div>
</template>
<script>
export default {
name: 'app',
data() {
return {
list: [{
name: '小穎',
age:22
}, {
name: '仔仔',
age:1
}, {
name: '黑妞',
age:1
}, {
name: '土豆',
age:1
}]
}
},
methods: {
deleteFun(index) {
this.$delete(this.list[index], 'age');
}
}
}
</script>
<style>
#app {
font-family: 'Avenir', Helvetica, Arial, sans-serif;
-webkit-font-smoothing: antialiased;
-moz-osx-font-smoothing: grayscale;
text-align: center;
color: #2c3e50;
margin-top: 60px;
}
</style>
總結(jié)
以上所述是小編給大家介紹的vue 實(shí)例方法和數(shù)據(jù),希望對(duì)大家有所幫助,如果大家有任何疑問請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
相關(guān)文章
vue3+vite實(shí)現(xiàn)在線預(yù)覽pdf功能
這篇文章主要為大家詳細(xì)介紹了如何通過vue3和vite實(shí)現(xiàn)在線預(yù)覽pdf功能,文中的示例代碼簡潔易懂,具有一定的借鑒價(jià)值,感興趣的小伙伴可以學(xué)習(xí)一下2023-10-10
關(guān)于在vue中實(shí)現(xiàn)過渡動(dòng)畫的代碼示例
Vue是一款流行的前端框架,支持過渡動(dòng)畫的實(shí)現(xiàn)是其中的一項(xiàng)重要特性,在Vue中,使用過渡動(dòng)畫可以為用戶提供更加友好的用戶體驗(yàn),下面我將為大家介紹一下子如何在Vue中實(shí)現(xiàn)過渡動(dòng)畫,需要的朋友可以參考下2023-06-06
vue.js移動(dòng)端tab組件的封裝實(shí)踐實(shí)例
本篇文章主要介紹了vue.js移動(dòng)端tab的封裝實(shí)踐實(shí)例,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-06-06
Vue開發(fā)項(xiàng)目中如何使用Font Awesome 5
Font Awesome是一套流行的圖標(biāo)字體庫,我們?cè)趯?shí)際開發(fā)的過程中會(huì)經(jīng)常遇到需要使用圖標(biāo)的場景,對(duì)于一些常用的圖標(biāo),我們可以直接在Font Awesome中找到并且使用,這篇文章主要給大家介紹了關(guān)于Vue開發(fā)項(xiàng)目中如何使用Font Awesome5的相關(guān)資料,需要的朋友可以參考下2021-11-11
ElementUI如何修改Dialog的標(biāo)題樣式
這篇文章主要介紹了ElementUI如何修改Dialog的標(biāo)題樣式問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-10-10
element-table如何實(shí)現(xiàn)自定義表格排序
這篇文章主要介紹了element-table如何實(shí)現(xiàn)自定義表格排序,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-07-07
淺談Vue static 靜態(tài)資源路徑 和 style問題
這篇文章主要介紹了淺談Vue static 靜態(tài)資源路徑 和 style問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-11-11

