溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

vue組件生有哪些命周期

發(fā)布時間:2021-07-09 14:32:23 來源:億速云 閱讀:106 作者:Leah 欄目:web開發(fā)

本篇文章給大家分享的是有關(guān)vue組件生有哪些命周期,小編覺得挺實用的,因此分享給大家學(xué)習(xí),希望大家閱讀完這篇文章后可以有所收獲,話不多說,跟著小編一起來看看吧。

具體內(nèi)容如下

分為4個階段:

create/mount/update/destroy

每一個階段都對應(yīng)著有自己的處理函數(shù)

create: beforeCreate created

初始化

mount: beforeMount mounted

和掛載相關(guān)的處理

update: beforeUpdate updated

根據(jù)要更新的數(shù)據(jù) 做邏輯判斷

destroy:beforeDestroy destroyed

清理工作

代碼:

<!doctype html>
<html>
 <head>
 <meta charset="UTF-8">
 <title>生命周期</title>
 <script src="js/vue.js"></script>
 </head>
 <body>
 <div id="container">
  <p>{{msg}}</p>
<!--點擊的時候isShow進行取反 -->
  <button @click="isShow = !isShow">切換是否顯示組件</button>
  <my-component v-if="isShow"></my-component>
 </div>
 <script>
  Vue.component("my-component",{
   template:`
     <div>
      <button @click="handleClick">Click Me</button>
      <h2>component:{{count}}</h2>
      </div>
   `,
   data:function(){
     return {
      count:0
     }
    },
   methods:{
    handleClick:function(){
     this.count++;
    }
   },
   beforeCreate: function () {
   console.log('準備創(chuàng)建組件');
  },
  created: function () {
   console.log('組件創(chuàng)建完畢');
  },
  beforeMount: function () {
   console.log('組件的模板準備掛載到DOM');
  },
  mounted: function () {
   console.log('掛載完畢');
  },
  beforeUpdate: function () {
   console.log('準備更新了');
  },
  updated:function(){
   console.log('更新完成');
  },
  beforeDestroy: function () {
   console.log('準備destroy');
  },
  destroyed: function () {
   console.log('destroy完成');
  }
  })
  new Vue({
   el:"#container",
   data:{
    msg:"Hello VueJs",
    isShow:true
   }
  })
 </script>
 </body>
</html>

生命周期練習(xí),需要那階段寫那個階段

<!doctype html>
<html>
 <head>
 <meta charset="UTF-8">
 <title>生命周期練習(xí)</title>
 <script src="js/vue.js"></script>
 </head>
 <body>
 <div id="container">
  <p>{{msg}}</p>
  <my-component></my-component>
 </div>
 <script>
  Vue.component("my-component",{
   data:function(){
    return {
     myOpacity:0
    }
   },
   template:` <h2 v-bind:>透明度將改變
   </h2>`,
   mounted:function(){
    setInterval(function(){
     this.myOpacity += 0.1;
     if(this.myOpacity>1){
      this.myOpacity = 0;
     }
    }.bind(this),1000)
   }
  })
  new Vue({
   el:"#container",
   data:{
    msg:"Hello VueJs"
   }
  })
 </script>
 </body>
</html>

以上就是vue組件生有哪些命周期,小編相信有部分知識點可能是我們?nèi)粘9ぷ鲿姷交蛴玫降?。希望你能通過這篇文章學(xué)到更多知識。更多詳情敬請關(guān)注億速云行業(yè)資訊頻道。

向AI問一下細節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

vue
AI