国产99久久精品_欧美日本韩国一区二区_激情小说综合网_欧美一级二级视频_午夜av电影_日本久久精品视频

最新文章專(zhuān)題視頻專(zhuān)題問(wèn)答1問(wèn)答10問(wèn)答100問(wèn)答1000問(wèn)答2000關(guān)鍵字專(zhuān)題1關(guān)鍵字專(zhuān)題50關(guān)鍵字專(zhuān)題500關(guān)鍵字專(zhuān)題1500TAG最新視頻文章推薦1 推薦3 推薦5 推薦7 推薦9 推薦11 推薦13 推薦15 推薦17 推薦19 推薦21 推薦23 推薦25 推薦27 推薦29 推薦31 推薦33 推薦35 推薦37視頻文章20視頻文章30視頻文章40視頻文章50視頻文章60 視頻文章70視頻文章80視頻文章90視頻文章100視頻文章120視頻文章140 視頻2關(guān)鍵字專(zhuān)題關(guān)鍵字專(zhuān)題tag2tag3文章專(zhuān)題文章專(zhuān)題2文章索引1文章索引2文章索引3文章索引4文章索引5123456789101112131415文章專(zhuān)題3
問(wèn)答文章1 問(wèn)答文章501 問(wèn)答文章1001 問(wèn)答文章1501 問(wèn)答文章2001 問(wèn)答文章2501 問(wèn)答文章3001 問(wèn)答文章3501 問(wèn)答文章4001 問(wèn)答文章4501 問(wèn)答文章5001 問(wèn)答文章5501 問(wèn)答文章6001 問(wèn)答文章6501 問(wèn)答文章7001 問(wèn)答文章7501 問(wèn)答文章8001 問(wèn)答文章8501 問(wèn)答文章9001 問(wèn)答文章9501
當(dāng)前位置: 首頁(yè) - 科技 - 知識(shí)百科 - 正文

詳解vue 實(shí)例方法和數(shù)據(jù)

來(lái)源:懂視網(wǎng) 責(zé)編:小采 時(shí)間:2020-11-27 22:27:21
文檔

詳解vue 實(shí)例方法和數(shù)據(jù)

詳解vue 實(shí)例方法和數(shù)據(jù):1.vm.$set 問(wèn)題描述: 如何在不通過(guò)循環(huán)數(shù)據(jù)給list數(shù)據(jù)添加一個(gè)showMore屬性,并且在moreFun中改變這個(gè)新增屬性的值,并實(shí)現(xiàn)雙向綁定? <template> <div id=app> <div class=demo> <ul> &
推薦度:
導(dǎo)讀詳解vue 實(shí)例方法和數(shù)據(jù):1.vm.$set 問(wèn)題描述: 如何在不通過(guò)循環(huán)數(shù)據(jù)給list數(shù)據(jù)添加一個(gè)showMore屬性,并且在moreFun中改變這個(gè)新增屬性的值,并實(shí)現(xiàn)雙向綁定? <template> <div id=app> <div class=demo> <ul> &

1.vm.$set

問(wèn)題描述:

如何在不通過(guò)循環(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>

一開(kāi)始小穎并不知道怎么做,而且小穎覺(jué)得               

 <div v-show="!v.showMore">
 <button @click="moreFun(index)">展示更多</button>
 </div>

這段代碼肯定會(huì)報(bào)錯(cuò),然而當(dāng)小穎寫(xiě)上后發(fā)現(xiàn),并沒(méi)有,后來(lái)那位帥鍋告訴我,看看vue的  vm.$set     小穎看后將moreFun方法寫(xiě)為:

 moreFun(index) {
 this.$set(this.list[index], 'showMore', true);
 console.log(this.list);
 }

然后就達(dá)到小穎想要的結(jié)果啦。小穎當(dāng)時(shí)遇到的問(wèn)題類(lèi)似于這樣的:

<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>

問(wèn)題:當(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ā)視圖更新。

所以后來(lái)小穎就將showMore直接添加到list中,然后就好啦。現(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ì)大家有所幫助,如果大家有任何疑問(wèn)請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!

聲明:本網(wǎng)頁(yè)內(nèi)容旨在傳播知識(shí),若有侵權(quán)等問(wèn)題請(qǐng)及時(shí)與本網(wǎng)聯(lián)系,我們將在第一時(shí)間刪除處理。TEL:177 7030 7066 E-MAIL:11247931@qq.com

文檔

詳解vue 實(shí)例方法和數(shù)據(jù)

詳解vue 實(shí)例方法和數(shù)據(jù):1.vm.$set 問(wèn)題描述: 如何在不通過(guò)循環(huán)數(shù)據(jù)給list數(shù)據(jù)添加一個(gè)showMore屬性,并且在moreFun中改變這個(gè)新增屬性的值,并實(shí)現(xiàn)雙向綁定? <template> <div id=app> <div class=demo> <ul> &
推薦度:
標(biāo)簽: 方法 數(shù)據(jù) VUE
  • 熱門(mén)焦點(diǎn)

最新推薦

猜你喜歡

熱門(mén)推薦

專(zhuān)題
Top
主站蜘蛛池模板: 亚洲国产成人久久99精品 | 国产日韩欧美精品一区二区三区 | 国产一区亚洲二区三区毛片 | 国产女上位 | 人善交另类欧美重口另类 | 精品国产欧美一区二区三区成人 | 日本专区| 国产麻豆久久 | 影音先锋亚洲综合小说在线 | 久久综合精品国产一区二区三区 | 国产高清一区二区 | 日韩精品网站 | 国产亚洲视频在线 | 欧美日韩中文在线 | 国产一区二区三区欧美 | 亚洲最大色网 | 免费一区二区三区在线视频 | 亚洲欧美日韩中文字幕在线 | 国产高清一区二区三区 | 免费观看欧美一区二区三区 | 欧美激情伊人 | 欧美阿v高清资源在线 | 亚洲欧洲精品成人久久曰影片 | 91久久精品国产91久久性色也 | 日韩经典第一页 | 国产日韩欧美第一页 | 久久国产亚洲欧美日韩精品 | 97伊人网 | 国产激情视频一区二区三区 | 性夜影院爽黄a爽免费看网站 | 亚洲精品在线免费看 | 国产精品久久久久久久久久久不卡 | 国产高清免费 | 国产精品免费大片一区二区 | 日本中文字幕有码 | 国产精品久久久久久久久久久久 | 中文字幕一区二区三区不卡 | 日本二区在线观看 | 国产精品久久久久… | 亚洲欧美影视 | 国产精品九九久久精品女同 |