最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

 更新時間:2017年10月23日 09:41:53   作者:愛喝酸奶的吃貨  
這篇文章主要介紹了vue 實例方法和數(shù)據(jù)的相關(guān)資料,非常不錯,具有參考借鑒價值,需要的朋友可以參考下

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

    這篇文章主要為大家詳細介紹了如何通過vue3和vite實現(xiàn)在線預(yù)覽pdf功能,文中的示例代碼簡潔易懂,具有一定的借鑒價值,感興趣的小伙伴可以學習一下
    2023-10-10
  • 關(guān)于在vue中實現(xiàn)過渡動畫的代碼示例

    關(guān)于在vue中實現(xiàn)過渡動畫的代碼示例

    Vue是一款流行的前端框架,支持過渡動畫的實現(xiàn)是其中的一項重要特性,在Vue中,使用過渡動畫可以為用戶提供更加友好的用戶體驗,下面我將為大家介紹一下子如何在Vue中實現(xiàn)過渡動畫,需要的朋友可以參考下
    2023-06-06
  • vue.js移動端tab組件的封裝實踐實例

    vue.js移動端tab組件的封裝實踐實例

    本篇文章主要介紹了vue.js移動端tab的封裝實踐實例,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-06-06
  • Vue開發(fā)項目中如何使用Font Awesome 5

    Vue開發(fā)項目中如何使用Font Awesome 5

    Font Awesome是一套流行的圖標字體庫,我們在實際開發(fā)的過程中會經(jīng)常遇到需要使用圖標的場景,對于一些常用的圖標,我們可以直接在Font Awesome中找到并且使用,這篇文章主要給大家介紹了關(guān)于Vue開發(fā)項目中如何使用Font Awesome5的相關(guān)資料,需要的朋友可以參考下
    2021-11-11
  • ElementUI如何修改Dialog的標題樣式

    ElementUI如何修改Dialog的標題樣式

    這篇文章主要介紹了ElementUI如何修改Dialog的標題樣式問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2023-10-10
  • element-table如何實現(xiàn)自定義表格排序

    element-table如何實現(xiàn)自定義表格排序

    這篇文章主要介紹了element-table如何實現(xiàn)自定義表格排序,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-07-07
  • vue單頁面改造多頁面應(yīng)用的全過程記錄

    vue單頁面改造多頁面應(yīng)用的全過程記錄

    眾所都知vue是一個單頁面應(yīng)用,下面這篇文章主要給大家介紹了關(guān)于vue單頁面改造多頁面應(yīng)用的相關(guān)資料,文中通過實例代碼介紹的非常詳細,需要的朋友可以參考下
    2022-05-05
  • vue中filter的應(yīng)用場景詳解

    vue中filter的應(yīng)用場景詳解

    這篇文章主要為大家介紹了vue中filter的應(yīng)用場景,具有一定的參考價值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來幫助
    2021-11-11
  • 淺談Vue static 靜態(tài)資源路徑 和 style問題

    淺談Vue static 靜態(tài)資源路徑 和 style問題

    這篇文章主要介紹了淺談Vue static 靜態(tài)資源路徑 和 style問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-11-11
  • vue3中的抽離封裝方法實現(xiàn)

    vue3中的抽離封裝方法實現(xiàn)

    vue3中的任何一個組合式api都可以單獨抽離出去在另一個文件,最后只需要回歸到setup()中即可,這篇文章主要介紹了vue3的抽離封裝方法,需要的朋友可以參考下
    2022-08-08

最新評論

仙游县| 陆丰市| 沐川县| 周宁县| 商河县| 东山县| 庆安县| 武定县| 新民市| 垣曲县| 婺源县| 泾源县| 六枝特区| 枝江市| 和林格尔县| 济阳县| 桐庐县| 怀仁县| 北辰区| 洛浦县| 屯门区| 咸阳市| 武平县| 海阳市| 龙州县| 大新县| 巴中市| 福泉市| 尖扎县| 淳化县| 伊宁市| 神池县| 高阳县| 洪洞县| 池州市| 清涧县| 依兰县| 灵寿县| 额济纳旗| 千阳县| 平乡县|