1. 程式人生 > 其它 >vue學習08——生命週期函式、setup函式

vue學習08——生命週期函式、setup函式

vue中所有的鉤子函式:

beforeCreate(建立前)
created(建立後)
beforeMount(載入前)
mounted(載入後)
beforeUpdate(更新前)
updated(更新後)
beforeDestroy(銷燬前)

下面是完整程式碼

<!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="https://cdn.bootcss.com/vue/2.4.2/vue.js"></script>
    </head>
 
    <body>
        <div id="app">
            <input v-model="message"></input>
            <h1>{{message1}}</h1>
        </div>
    </body>
    <script>
        var
vm = new Vue({/*建立vue物件*/ el: '#app',/****掛載目標****/ data: {/****資料物件****/ message: 'Hello World!' }, computed:{/****實現某一屬性的實時計算****/ message1:function(){ return this.message.split("").reverse().join(""); } }, watched:{
/****檢測某一屬性值的變化****/ }, methods:{/****元件內部的方法****/ }, beforeCreate: function() { console.group('------beforeCreate建立前狀態------'); console.log("%c%s", "color:red", "el : " + this.$el); //undefined console.log("%c%s", "color:red", "data : " + this
.$data); //undefined console.log("%c%s", "color:red", "message: " + this.message)//undefined }, /** * 1.在beforeCreate和created鉤子之間,程式開始監控Data物件資料的變化及vue內部的初始化事件 * * */ created: function() { console.group('------created建立完畢狀態------'); console.log("%c%s", "color:red", "el : " + this.$el); //undefined console.log("%c%s", "color:red", "data : " + this.$data); //已被初始化 console.log("%c%s", "color:red", "message: " + this.message); //已被初始化 }, /** * 2.在created和beforeMount之間,判斷是否有el選項,若有則繼續編譯,無,則暫停生命週期; * 然後程式會判斷是否有templete引數選項,若有,則將其作為模板編譯成render函式。若無,則將外部html作為模板編譯(template優先順序比外部html高) * * */ beforeMount: function() { console.group('------beforeMount掛載前狀態------'); console.log("%c%s", "color:red", "el : " + (this.$el)); //已被初始化 console.log(this.$el); console.log("%c%s", "color:red", "data : " + this.$data); //已被初始化 console.log("%c%s", "color:red", "message: " + this.message); //已被初始化 }, /** * 3.在beforeMount和mounted之間,程式將上一步編輯好的html內容替換el屬性指向的dom物件或者選擇權對應的html標籤裡面的內容 * * */ mounted: function() { console.group('------mounted 掛載結束狀態------'); console.log("%c%s", "color:red", "el : " + this.$el); //已被初始化 console.log(this.$el); console.log("%c%s", "color:red", "data : " + this.$data); //已被初始化 console.log("%c%s", "color:red", "message: " + this.message); //已被初始化 }, /** * 4.mounted和beforeUpdate之間,程式實時監控資料變化 * * */ beforeUpdate: function() { console.group('beforeUpdate 更新前狀態===============》'); console.log("%c%s", "color:red", "el : " + this.$el); console.log(this.$el); console.log("%c%s", "color:red", "data : " + this.$data); console.log("%c%s", "color:red", "message: " + this.message); }, /** * 5.beforeUpdate和updated之間,實時更新dom * * */ updated: function() { console.group('updated 更新完成狀態===============》'); console.log("%c%s", "color:red", "el : " + this.$el); console.log(this.$el); console.log("%c%s", "color:red", "data : " + this.$data); console.log("%c%s", "color:red", "message: " + this.message); }, beforeDestroy: function() { console.group('beforeDestroy 銷燬前狀態===============》'); console.log("%c%s", "color:red", "el : " + this.$el); console.log(this.$el); console.log("%c%s", "color:red", "data : " + this.$data); console.log("%c%s", "color:red", "message: " + this.message); }, /** * 6.例項銷燬 * * */ destroyed: function() { console.group('destroyed 銷燬完成狀態===============》'); console.log("%c%s", "color:red", "el : " + this.$el); console.log(this.$el); console.log("%c%s", "color:red", "data : " + this.$data); console.log("%c%s", "color:red", "message: " + this.message) } }) </script> </html>

 destroyed(銷燬後)

setup函式的特性以及作用

  可以確定的是 Vue3.0 是相容 Vue2.x 版本的 也就是說我們再日常工作中 可以在 Vue3 中使用 Vue2.x 的相關語法 但是當你真正開始使用 Vue3 寫專案時 你會發現他比 Vue2.x 方便的多

  Vue3 的一大特性函式 ---- setup

  1、setup函式是處於 生命週期函式 beforeCreate 和 Created 兩個鉤子函式之間的函式 也就說在 setup函式中是無法 使用 data 和 methods 中的資料和方法的

  2、setup函式是 Composition API(組合API)的入口

  3、在setup函式中定義的變數和方法最後都是需要 return 出去的 不然無法再模板中使用

setup函式的注意點

  1、由於在執行 setup函式的時候,還沒有執行 Created 生命週期方法,所以在 setup 函式中,無法使用 data 和 methods 的變數和方法

  2、由於我們不能在 setup函式中使用 data 和 methods,所以 Vue 為了避免我們錯誤的使用,直接將 setup函式中的this修改成了 undefined

  3、setup函式只能是同步的不能是非同步的

搜尋

複製