Vue中屬性、方法、生命周期的示例分析-創(chuàng)新互聯(lián)

這篇文章主要介紹了Vue中屬性、方法、生命周期的示例分析,具有一定借鑒價(jià)值,感興趣的朋友可以參考下,希望大家閱讀完這篇文章之后大有收獲,下面讓小編帶著大家一起了解一下。

站在用戶的角度思考問題,與客戶深入溝通,找到鐵西網(wǎng)站設(shè)計(jì)與鐵西網(wǎng)站推廣的解決方案,憑借多年的經(jīng)驗(yàn),讓設(shè)計(jì)與互聯(lián)網(wǎng)技術(shù)結(jié)合,創(chuàng)造個(gè)性化、用戶體驗(yàn)好的作品,建站類型包括:網(wǎng)站建設(shè)、網(wǎng)站制作、企業(yè)官網(wǎng)、英文網(wǎng)站、手機(jī)端網(wǎng)站、網(wǎng)站推廣、主機(jī)域名雅安服務(wù)器托管、企業(yè)郵箱。業(yè)務(wù)覆蓋鐵西地區(qū)。

實(shí)例

<!DOCTYPE html>
  <html lang="en">
   <head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Vue的屬性、方法和生命周期</title>
    <script src="Vue.min.js"></script>
   </head>
  
   <body>
    <div id="main">
     <span>{{ message }}</span>
     <br/>
     <span>{{ number }}</span>
     <br/>
     <button v-on:click="add">add</button>
    </div>
   </body>
  </html>
  
  <script>
   const App = new Vue({
    // 選擇器
    el: '#main',
    // 數(shù)據(jù)
    data: {
     // 在data里面不僅可以定義字符串,我們還可以定義number
     message: 'Welcome to Chivalrous Island!',
     number: 85,
    },
    // 如果我們從服務(wù)器得到的數(shù)據(jù)并不是我們需要的,可能是上面數(shù)據(jù)的結(jié)合,這時(shí)我們可以用到一個(gè)Vue提供的一個(gè)屬性:計(jì)算屬性
    // 計(jì)算屬性:可以把data里面的數(shù)據(jù)計(jì)算一下,然后返回一個(gè)新的數(shù)據(jù),它被叫做computed。
    computed: {
     // 可以定義函數(shù),然后返回需要的數(shù)據(jù),比如下面我們要得到上面number的平方,計(jì)算結(jié)果為:
     getSqure: function () {
       return this.number * this.number;
     }
    },
    // 定義函數(shù)
    methods: {
     add: function() {
      this.number++;
     }
    },
    // 監(jiān)聽屬性(監(jiān)聽器),它可以監(jiān)聽一個(gè)函數(shù)或者是一個(gè)變量
    watch: {
     // 函數(shù)接收兩個(gè)參數(shù)值,afterVal代表改變之后的值,beforeVal表示改變之前的值
     number: function(afterVal,beforeVal) {
      console.log('beforeVal',beforeVal);
      console.log('afterVal',afterVal);
     }
    }
   });
  
   // 打印出來的結(jié)果
   console.log(App.getSqure);
  </script>

屬性

從上面的案例可以知道,屬性可以分為計(jì)算屬性(computed)和監(jiān)聽屬性(watch)。

計(jì)算屬性有一個(gè)好處在于它有一個(gè)緩存機(jī)制,因此它不需要每次都重新計(jì)算。

監(jiān)聽屬性(監(jiān)聽器),它可以監(jiān)聽一個(gè)函數(shù)或者是一個(gè)變量。

Vue中屬性、方法、生命周期的示例分析 

方法(methods)

methods常調(diào)用的函數(shù)。

上面的示例中,getSqure,add,number,像這些都是我們自定義的方法。

生命周期(鉤子函數(shù))

生命周期就是從它開始創(chuàng)建到銷毀經(jīng)歷的過程。

這個(gè)生命周期也就是Vue的實(shí)例,從開始創(chuàng)建,到創(chuàng)建完成,到掛載,再到更新,然后再銷毀的一系列過程,這個(gè)官方有一個(gè)說法也叫作鉤子函數(shù)。

<script>
  window.onload = () => {
    const App = new Vue({
      ......
    
      // 生命周期第一步:創(chuàng)建前(vue實(shí)例還未創(chuàng)建)
       beforeCreate() {
         // %c 相當(dāng)于給輸出結(jié)果定義一個(gè)樣式
        console.log('%cbeforeCreate','color:green', this.$el);
        console.log('%cbeforeCreate','color:green', this.message);
       },
      // 創(chuàng)建完成
      created() {
         console.log('%ccreated','color:red', this.$el);
         console.log('%ccreated','color:red', this.message);
      },  
      // 掛載之前
      beforeMount() {
        console.log('%cbeforeMount','color:blue', this.$el);
        console.log('%cbeforeMount','color:blue', this.message);
      },
      // 已經(jīng)掛載但是methods里面的方法還沒有執(zhí)行,從創(chuàng)建到掛載全部完成
      mounted() {
        console.log('%cmounted','color:orange', this.$el);
        console.log('%cmounted','color:orange', this.message);
      },
       // 創(chuàng)建完之后,數(shù)據(jù)更新前
      beforeUpdate() {
        console.log('%cbeforeUpdate','color:#f44586', this.$el);
        console.log('%cbeforeUpdate','color:#f44586', this.number);
      },
      // 數(shù)據(jù)全部更新完成
      updated() {
        console.log('%cupdated','color:olive', this.$el);
        console.log('%cupdated','color:olive', this.number);
      },
      // 銷毀
      beforeDestroy() {
        console.log('%cbeforeDestroy','color:gray', this.$el);
        console.log('%cbeforeDestroy','color:gray', this.number);
      },
      destroyed() {
        console.log('%cdestroyed','color:yellow', this.$el);
        console.log('%cdestroyed','color:yellow', this.number);
      }
    });
  // 打印出來的結(jié)果
    console.log(App.getSqure);
    window.App = App;
  };
  
  // 銷毀vue實(shí)例
  function destroy() {
    App.$destroy();
  }
  </script>

html:

<body>
    <div id="main">
     <span>{{ message }}</span>
     <br/>
     <span>{{ number }}</span>
     <br/>
     <button v-on:click="add">add</button>
      <br />
     <button Onclick="destroy()">destroy</button>
    </div>
  </body>

Vue中屬性、方法、生命周期的示例分析

Vue中屬性、方法、生命周期的示例分析

感謝你能夠認(rèn)真閱讀完這篇文章,希望小編分享的“Vue中屬性、方法、生命周期的示例分析”這篇文章對大家有幫助,同時(shí)也希望大家多多支持創(chuàng)新互聯(lián)成都網(wǎng)站設(shè)計(jì)公司,關(guān)注創(chuàng)新互聯(lián)成都網(wǎng)站設(shè)計(jì)公司行業(yè)資訊頻道,更多相關(guān)知識(shí)等著你來學(xué)習(xí)!

另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)scvps.cn,海內(nèi)外云服務(wù)器15元起步,三天無理由+7*72小時(shí)售后在線,公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、網(wǎng)站設(shè)計(jì)器、香港服務(wù)器、美國服務(wù)器、虛擬主機(jī)、免備案服務(wù)器”等云主機(jī)租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡單易用、服務(wù)可用性高、性價(jià)比高”等特點(diǎn)與優(yōu)勢,專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應(yīng)用場景需求。

新聞標(biāo)題:Vue中屬性、方法、生命周期的示例分析-創(chuàng)新互聯(lián)
文章位置:http://muchs.cn/article10/dpppgo.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站改版、手機(jī)網(wǎng)站建設(shè)、用戶體驗(yàn)動(dòng)態(tài)網(wǎng)站、網(wǎng)頁設(shè)計(jì)公司、品牌網(wǎng)站建設(shè)

廣告

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

網(wǎng)站托管運(yùn)營