国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 語言 > JavaScript > 正文

詳解vue 實例方法和數據

2024-05-06 15:25:17
字體:
來源:轉載
供稿:網友

1.vm.$set

問題描述:

如何在不通過循環數據給list數據添加一個showMore屬性,并且在moreFun中改變這個新增屬性的值,并實現雙向綁定?

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

這段代碼肯定會報錯,然而當小穎寫上后發現,并沒有,后來那位帥鍋告訴我,看看vue的  vm.$set     小穎看后將moreFun方法寫為:

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

然后就達到小穎想要的結果啦。小穎當時遇到的問題類似于這樣的:

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

問題:當執行完moreFun方法后,雖然list中的showMore屬性的值變成了true,但是

<div v-show="!v.showMore"> <button @click="moreFun(index)">展示更多</button> </div>            
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表

圖片精選

主站蜘蛛池模板: 诏安县| 米脂县| 广州市| 乐安县| 萝北县| 万源市| 梁平县| 拉萨市| 绥德县| 平阴县| 崇文区| 常山县| 平顶山市| 阿勒泰市| 霍山县| 铁岭县| 厦门市| 历史| 都昌县| 营口市| 弥勒县| 全州县| 枝江市| 翁牛特旗| 台湾省| 潼南县| 青铜峡市| 恩施市| 平泉县| 杭锦后旗| 民县| 九龙县| 吴忠市| 徐水县| 澜沧| 汕尾市| 安远县| 柳州市| 古浪县| 安化县| 富宁县|