亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

vue3.0生命周期的示例代碼

 更新時(shí)間:2020年09月24日 10:49:04   作者:碼奴吳彥祖  
這篇文章主要介紹了vue3.0生命周期的相關(guān)知識(shí),本文通過示例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友參考下吧

在組件化的框架中,比如Angular、React或Vue,都為組件定義了生命周期這個(gè)概念,每個(gè)組件實(shí)例在被創(chuàng)建時(shí)都要經(jīng)過一系列的初始化過程,例如:需要設(shè)置數(shù)據(jù)監(jiān)聽、編譯模板、將實(shí)例掛載到 DOM 并在數(shù)據(jù)變化時(shí)更新 DOM 等。同時(shí),在這個(gè)過程中也會(huì)運(yùn)行一些叫做生命周期鉤子的函數(shù),它們提供給用戶在組件的不同階段添加自己的代碼的機(jī)會(huì)。
使用過Vue2.x的朋友肯定對(duì)它的生命周期鉤子很熟悉了,因?yàn)樵趯?shí)際的開發(fā)過程中我們多多少少會(huì)用到他們,比如 created、mounted、destoryed等等。而在Vue3.0中,Vue2.x Options API形式的生命周期鉤子函數(shù)和新的Composition API都可以使用,來看個(gè)示例代碼就明白了:

const { onMounted } = Vue

const MyComp = {
  
  // Options API
  mounted() {
    console.log('>>>>>> mounted 1')
  },
  
  setup() {
    // Composition API
    onMounted(() => {
      console.log('++++++ mounted 2')
    })
  }
}

兩種形式的生命周期函數(shù)可以共存(當(dāng)然實(shí)際使用的時(shí)候最好只選用一種),它們都會(huì)被執(zhí)行。Composition API形式的生命周期函數(shù)都是在 setup 方法中被調(diào)用注冊(cè)。
最后,在實(shí)際的開發(fā)過程中,請(qǐng)注意一下Options API形式的組件生命周期鉤子和Composition API之間的實(shí)際對(duì)應(yīng)關(guān)系:

beforeCreate -> 請(qǐng)使用 setup()
created -> 請(qǐng)使用 setup()
beforeMount -> onBeforeMount
mounted -> onMounted
beforeUpdate -> onBeforeUpdate
updated -> onUpdated
beforeDestroy -> onBeforeUnmount
destroyed -> onUnmounted
errorCaptured -> onErrorCaptured

整體代碼如下:

const { createComponent, createApp, reactive } = Vue

// 計(jì)數(shù)器組件
const Counter = {
  props: {
    initCount: {
      type: Number,
      default: 0
    }
  },
  template: `
    <div class="counter-display">
      <span class="counter-label">恭喜你,你已經(jīng)寫了</span>
      <span class="counter-text">{{ state.count }}</span>
      <span class="counter-label">斤代碼!</span>
    </div>
    <div class="counter-btns">
      <button class="btn" @click="increase">寫一斤</button>
      <button class="btn" @click="reset">刪庫(kù)啦</button>
    </div>
  `,
  // 相當(dāng)于 vue2.x beforeCreated, created
  setup(props) {
    const countOps = useCount(props.initCount)
    console.log("Counter ===> 相當(dāng)于 vue2.x 中 beforeCreated, created")
    return { ...countOps }
  },
  onBeforeMount() {
    console.log("Counter ===> 相當(dāng)于 vue2.x 中 beforeMount")
  },
  onMounted() {
    console.log("Counter ===> 相當(dāng)于 vue2.x 中 mounted")
  },
  onBeforeUpdate() {
    console.log("Counter ===> 相當(dāng)于 vue2.x 中 beforeUpdate")
  },
  onUpdated() {
    console.log("Counter ===> 相當(dāng)于 vue2.x 中 updated")
  },
  onBeforeUnmount() {
    console.log("Counter ===> 相當(dāng)于 vue2.x 中 beforeDestroy")
  },
  onUnmounted() {
    console.log("Counter ===> 相當(dāng)于 vue2.x 中 destroyed")
  },
  onErrorCaptured() {
    console.log("Counter ===> 相當(dāng)于 vue2.x 中 errorCaptured")
  }
}

function useCount(initCount) {
  const state = reactive({ count: initCount })
  console.log("state reactive", state)
  const increase = () => { state.count++ }
  const reset = () => { state.count = 0 }
  return { state, increase, reset }
}

// 創(chuàng)建一個(gè) 跟組件 app
const App = createComponent({
  // 這個(gè)就相對(duì)于 在另一個(gè) .vue 文件 引用 Counter 組件,需要在 components 屬性局部注冊(cè)組件
  components: {
    Counter,
  },
  // 掛載到 App 模板中
  template: `
    <div class="container">
      <h3>計(jì)數(shù)器示例</h3>
      <Counter />
    </div>
  `,
  setup() {
    console.log("App ===> 相當(dāng)于 vue2.x 中 beforeCreated, created")
  },
  onBeforeMount() {
    console.log("App ===> 相當(dāng)于 vue2.x 中 beforeMount")
  },
  onMounted() {
    console.log("App ===> 相當(dāng)于 vue2.x 中 mounted")
  },
  onBeforeUpdate() {
    console.log("App ===> 相當(dāng)于 vue2.x 中 beforeUpdate")
  },
  onUpdated() {
    console.log("App ===> 相當(dāng)于 vue2.x 中 updated")
  },
  onBeforeUnmount() {
    console.log("App ===> 相當(dāng)于 vue2.x 中 beforeDestroy")
  },
  onUnmounted() {
    console.log("App ===> 相當(dāng)于 vue2.x 中 destroyed")
  },
  onErrorCaptured() {
    console.log("Counter ===> 相當(dāng)于 vue2.x 中 errorCaptured")
  }
})

// 啟動(dòng)
// container 就是相當(dāng)于 new Vue() 中 el 元素
const container = document.querySelector("#app")
// createApp() 就是相當(dāng)于 new Vue() 內(nèi)部返回的就是 new Vue()
const app = createApp()
// 掛載組件
app.mount(App, container)

轉(zhuǎn)載自:https://zhuanlan.zhihu.com/p/95968847

到此這篇關(guān)于vue3.0生命周期的示例代碼的文章就介紹到這了,更多相關(guān)vue3.0生命周期內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • vue+iview如何實(shí)現(xiàn)拼音、首字母、漢字模糊搜索

    vue+iview如何實(shí)現(xiàn)拼音、首字母、漢字模糊搜索

    這篇文章主要介紹了vue+iview如何實(shí)現(xiàn)拼音、首字母、漢字模糊搜索,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-04-04
  • 淺談父子組件傳值問題

    淺談父子組件傳值問題

    這篇文章主要介紹了Vue父子組件傳值問題,文章中有詳細(xì)的示例代碼,感興趣的同學(xué)可以參考閱讀
    2023-04-04
  • vue中實(shí)現(xiàn)展示與隱藏側(cè)邊欄功能

    vue中實(shí)現(xiàn)展示與隱藏側(cè)邊欄功能

    這篇文章主要介紹了vue中實(shí)現(xiàn)展示與隱藏側(cè)邊欄功能,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2024-08-08
  • Vue項(xiàng)目中封裝組件的簡(jiǎn)單步驟記錄

    Vue項(xiàng)目中封裝組件的簡(jiǎn)單步驟記錄

    眾所周知組件(component)是vue.js最強(qiáng)大的功能之一,它可以實(shí)現(xiàn)功能的復(fù)用,以及對(duì)其他邏輯的解耦,下面這篇文章主要給大家介紹了關(guān)于Vue項(xiàng)目中封裝組件的相關(guān)資料,需要的朋友可以參考下
    2021-09-09
  • Vue3使用echarts繪制儀表盤

    Vue3使用echarts繪制儀表盤

    這篇文章主要為大家學(xué)習(xí)介紹了Vue3如何使用echarts實(shí)現(xiàn)繪制儀表盤,文中的示例代碼積極學(xué)習(xí),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下
    2023-07-07
  • vue中使用echarts制作圓環(huán)圖的實(shí)例代碼

    vue中使用echarts制作圓環(huán)圖的實(shí)例代碼

    這篇文章主要介紹了vue中使用echarts制作圓環(huán)圖的實(shí)例代碼,代碼簡(jiǎn)單易懂,非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2018-07-07
  • Vue中自定義標(biāo)簽及其使用方式

    Vue中自定義標(biāo)簽及其使用方式

    這篇文章主要介紹了Vue中自定義標(biāo)簽及其使用方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-06-06
  • 前端vue項(xiàng)目debugger調(diào)試操作詳解

    前端vue項(xiàng)目debugger調(diào)試操作詳解

    在vue項(xiàng)目調(diào)試的時(shí)候,代碼里面標(biāo)注debugger,這篇文章主要給大家介紹了關(guān)于前端vue項(xiàng)目debugger調(diào)試操作的相關(guān)資料,文中通過圖文介紹的非常詳細(xì),需要的朋友可以參考下
    2024-05-05
  • Vue3實(shí)現(xiàn)粒子動(dòng)態(tài)背景的示例詳解

    Vue3實(shí)現(xiàn)粒子動(dòng)態(tài)背景的示例詳解

    這篇文章主要為大家詳細(xì)介紹了如何利用Vue3實(shí)現(xiàn)粒子動(dòng)態(tài)背景,文中的示例代碼講解詳細(xì),具有一定的學(xué)習(xí)價(jià)值,感興趣的小伙伴可以跟隨小編一起了解一下
    2023-11-11
  • 詳談vue+webpack解決css引用圖片打包后找不到資源文件的問題

    詳談vue+webpack解決css引用圖片打包后找不到資源文件的問題

    下面小編就為大家分享一篇詳談vue+webpack解決css引用圖片打包后找不到資源文件的問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2018-03-03

最新評(píng)論