成人午夜视频全免费观看高清-秋霞福利视频一区二区三区-国产精品久久久久电影小说-亚洲不卡区三一区三区一区

vue中實例方法和數(shù)據(jù)的示例分析

小編給大家分享一下vue中實例方法和數(shù)據(jù)的示例分析,相信大部分人都還不怎么了解,因此分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后大有收獲,下面讓我們一起去了解一下吧!

成都創(chuàng)新互聯(lián)公司是一家專業(yè)提供黃島企業(yè)網(wǎng)站建設,專注與網(wǎng)站設計、網(wǎng)站制作、H5技術、小程序制作等業(yè)務。10年已為黃島眾多企業(yè)、政府機構等服務。創(chuàng)新互聯(lián)專業(yè)網(wǎng)站建設公司優(yōu)惠進行中。

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);
  }

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

<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ù)?;卣{函數(shù)得到的參數(shù)為新值和舊值。表達式只接受監(jiān)督的鍵路徑。對于更復雜的表達式,用一個函數(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>

以上是“vue中實例方法和數(shù)據(jù)的示例分析”這篇文章的所有內容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內容對大家有所幫助,如果還想學習更多知識,歡迎關注創(chuàng)新互聯(lián)行業(yè)資訊頻道!

新聞名稱:vue中實例方法和數(shù)據(jù)的示例分析
網(wǎng)頁URL:http://jinyejixie.com/article36/johjpg.html

成都網(wǎng)站建設公司_創(chuàng)新互聯(lián),為您提供定制開發(fā)、電子商務、網(wǎng)站制作、企業(yè)建站、品牌網(wǎng)站建設、移動網(wǎng)站建設

廣告

聲明:本網(wǎng)站發(fā)布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經(jīng)允許不得轉載,或轉載時需注明來源: 創(chuàng)新互聯(lián)

h5響應式網(wǎng)站建設
枝江市| 安义县| 济阳县| 乐平市| 乳山市| 孟津县| 岳西县| 开原市| 莫力| 湛江市| 垫江县| 清涧县| 永平县| 长寿区| 林口县| 井研县| 灵寿县| 广州市| 共和县| 巩留县| 连城县| 栖霞市| 蒲江县| 遂溪县| 平塘县| 云安县| 田东县| 邻水| 固始县| 阆中市| 崇仁县| 长汀县| 柘城县| 扎赉特旗| 舞阳县| 沾化县| 沭阳县| 乌鲁木齐市| 宿州市| 唐海县| 湘乡市|