詳解vue 實例方法和數(shù)據(jù)
1.vm.$set
問題描述:
如何在不通過循環(huán)數(shù)據(jù)給list數(shù)據(jù)添加一個showMore屬性,并且在moreFun中改變這個新增屬性的值,并實現(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>
這段代碼肯定會報錯,然而當小穎寫上后發(fā)現(xiàn),并沒有,后來那位帥鍋告訴我,看看vue的 vm.$set 小穎看后將moreFun方法寫為:
moreFun(index) {
this.$set(this.list[index], 'showMore', true);
console.log(this.list);
}
然后就達到小穎想要的結(jié)果啦。小穎當時遇到的問題類似于這樣的:
<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>
問題:當執(zhí)行完moreFun方法后,雖然list中的showMore屬性的值變成了true,但是
<div v-show="!v.showMore"> <button @click="moreFun(index)">展示更多</button> </div>
按鈕 展示更多 仍然顯示著,這是因為,如果在實例創(chuàng)建之后添加新的屬性到實例上,它不會觸發(fā)視圖更新。
所以后來小穎就將showMore直接添加到list中,然后就好啦?,F(xiàn)在想想其實用個vm.$set就解決啦。
2.vm.$watch
用法:
觀察 Vue 實例變化的一個表達式或計算屬性函數(shù)?;卣{(diào)函數(shù)得到的參數(shù)為新值和舊值。表達式只接受監(jiān)督的鍵路徑。對于更復(fù)雜的表達式,用一個函數(shù)取代。
注意:在變異 (不是替換) 對象或數(shù)組時,舊值將與新值相同,因為它們的引用指向同一個對象/數(shù)組。Vue 不會保留變異之前值的副本。
<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ù)據(jù),希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復(fù)大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!
相關(guān)文章
vue3+vite實現(xiàn)在線預(yù)覽pdf功能
這篇文章主要為大家詳細介紹了如何通過vue3和vite實現(xiàn)在線預(yù)覽pdf功能,文中的示例代碼簡潔易懂,具有一定的借鑒價值,感興趣的小伙伴可以學習一下2023-10-10
關(guān)于在vue中實現(xiàn)過渡動畫的代碼示例
Vue是一款流行的前端框架,支持過渡動畫的實現(xiàn)是其中的一項重要特性,在Vue中,使用過渡動畫可以為用戶提供更加友好的用戶體驗,下面我將為大家介紹一下子如何在Vue中實現(xiàn)過渡動畫,需要的朋友可以參考下2023-06-06
Vue開發(fā)項目中如何使用Font Awesome 5
Font Awesome是一套流行的圖標字體庫,我們在實際開發(fā)的過程中會經(jīng)常遇到需要使用圖標的場景,對于一些常用的圖標,我們可以直接在Font Awesome中找到并且使用,這篇文章主要給大家介紹了關(guān)于Vue開發(fā)項目中如何使用Font Awesome5的相關(guān)資料,需要的朋友可以參考下2021-11-11
element-table如何實現(xiàn)自定義表格排序
這篇文章主要介紹了element-table如何實現(xiàn)自定義表格排序,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-07-07
淺談Vue static 靜態(tài)資源路徑 和 style問題
這篇文章主要介紹了淺談Vue static 靜態(tài)資源路徑 和 style問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-11-11

