日韩无码专区无码一级三级片|91人人爱网站中日韩无码电影|厨房大战丰满熟妇|AV高清无码在线免费观看|另类AV日韩少妇熟女|中文日本大黄一级黄色片|色情在线视频免费|亚洲成人特黄a片|黄片wwwav色图欧美|欧亚乱色一区二区三区

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時(shí)間:8:30-17:00
你可能遇到了下面的問題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
一篇文章上手Vue3中新增的API

1. 初始化項(xiàng)目

創(chuàng)新互聯(lián)建站是網(wǎng)站建設(shè)技術(shù)企業(yè),為成都企業(yè)提供專業(yè)的成都網(wǎng)站制作、網(wǎng)站設(shè)計(jì)、外貿(mào)網(wǎng)站建設(shè),網(wǎng)站設(shè)計(jì),網(wǎng)站制作,網(wǎng)站改版等技術(shù)服務(wù)。擁有十多年豐富建站經(jīng)驗(yàn)和眾多成功案例,為您定制適合企業(yè)的網(wǎng)站。十多年品質(zhì),值得信賴!

 
 
 
  1. // ① npm i -g @vue/cli 
  2. // ② vue create my-project 
  3. // ③ npm install @vue/composition-api -S 
  4.  
  5. // ④ main,js 
  6. import Vue from 'vue' 
  7. import VueCompositionApi from '@vue/composition-api' 
  8. Vue.use(VueCompositionApi) 

2. setup方法
setup是vue3.x中新的操作組件屬性的方法,它是組件內(nèi)部暴露出所有的屬性和方法的統(tǒng)一API。

2.1 執(zhí)行時(shí)機(jī)
setup的執(zhí)行時(shí)機(jī)在:beforeCreate 之后 created之前

 
 
 
  1. setup(props, ctx) { 
  2.     console.log('setup') 
  3.   }, 
  4.   beforeCreate() { 
  5.     console.log('beforeCreate') 
  6.   }, 
  7.   created() { 
  8.     console.log('created') 
  9.   }, 

2.2 接受props數(shù)據(jù)

 
 
 
  1.  
  2.  

 
 
 
  1. // 通過 setup 函數(shù)的第一個(gè)形參,接收 props 數(shù)據(jù): 
  2. setup(props) { 
  3.   console.log(props) 
  4. }, 
  5. // 在 props 中定義當(dāng)前組件允許外界傳遞過來的參數(shù)名稱: 
  6. props: { 
  7.     p1: String 
  8. /* 
  9. {} 
  10. p1: "傳值給 com-setup" 
  11. get p1: ? reactiveGetter() 
  12. set p1: ? reactiveSetter(newVal) 
  13. __proto__: Object 
  14. */ 

2.3 context
setup 函數(shù)的第二個(gè)形參是一個(gè)上下文對象,這個(gè)上下文對象中包含了一些有用的屬性,這些屬性在 vue 2.x 中需要通過 this 才能訪問到,在 vue 3.x 中,它們的訪問方式如下:

 
 
 
  1. setup(props, ctx) { 
  2.     console.log(ctx) 
  3.     console.log(this) // undefined 
  4.   }, 
  5. /* 
  6. attrs: Object 
  7. emit: ? () 
  8. listeners: Object 
  9. parent: VueComponent 
  10. refs: Object 
  11. root: Vue 
  12. ... 
  13. */ 

注意:在 setup() 函數(shù)中無法訪問到 this

3. reactive
reactive函數(shù)接收一個(gè)普通函數(shù),返回一個(gè)響應(yīng)式的數(shù)據(jù)對象。

reactive函數(shù)等價(jià)于 vue 2.x 中的 Vue.observable() 函數(shù),vue 3.x 中提供了 reactive() 函數(shù),用來創(chuàng)建響應(yīng)式的數(shù)據(jù)對象,基本代碼示例如下:

 
 
 
  1.  
  2.  
  3.  

4. ref
ref() 函數(shù)用來根據(jù)給定的值創(chuàng)建一個(gè)響應(yīng)式的數(shù)據(jù)對象,ref() 函數(shù)調(diào)用的返回值是一個(gè)對象,這個(gè)對象上只包含一個(gè) .value 屬性:

 
 
 
  1.  
  2.  
  3.  

4.1 在 reactive 對象中訪問 ref 創(chuàng)建的響應(yīng)式數(shù)據(jù)
當(dāng)把 ref() 創(chuàng)建出來的響應(yīng)式數(shù)據(jù)對象,掛載到 reactive() 上時(shí),會(huì)自動(dòng)把響應(yīng)式數(shù)據(jù)對象展開為原始的值,不需通過 .value 就可以直接被訪問,例如:

 
 
 
  1. setup() { 
  2.   const refCount = ref(0) 
  3.   const state = reactive({refCount}) 
  4.   console.log(state.refCount) // 輸出 0 
  5.   state.refCount++ // 此處不需要通過 .value 就能直接訪問原始值 
  6.   console.log(refCount) // 輸出 1 
  7.   return { 
  8.     refCount 
  9.   } 

注意:新的 ref 會(huì)覆蓋舊的 ref,示例代碼如下:

 
 
 
  1. setup() { 
  2.   // 創(chuàng)建 ref 并掛載到 reactive 中 
  3.   const c1 = ref(0); 
  4.   const state = reactive({ c1 }); 
  5.  
  6.   // 再次創(chuàng)建 ref,命名為 c2 
  7.   const c2 = ref(9); 
  8.   // 將 舊 ref c1 替換為 新 ref c2 
  9.   state.c1 = c2; 
  10.   state.c1++; 
  11.  
  12.   console.log(state.c1); // 輸出 10 
  13.   console.log(c2.value); // 輸出 10 
  14.   console.log(c1.value); // 輸出 0 

5. isRef
isRef() 用來判斷某個(gè)值是否為 ref() 創(chuàng)建出來的對象;應(yīng)用場景:當(dāng)需要展開某個(gè)可能為 ref() 創(chuàng)建出來的值的時(shí)候,例如:

 
 
 
  1. import { ref, reactive, isRef } from "@vue/composition-api"; 
  2. export default { 
  3.   setup() { 
  4.     const unwrapped = isRef(foo) ? foo.value : foo 
  5.   } 
  6. }; 

6. toRefs
toRefs() 函數(shù)可以將 reactive() 創(chuàng)建出來的響應(yīng)式對象,轉(zhuǎn)換為普通的對象,只不過,這個(gè)對象上的每個(gè)屬性節(jié)點(diǎn),都是 ref() 類型的響應(yīng)式數(shù)據(jù)。

 
 
 
  1.  
  2.  
  3.  

7. computed計(jì)算屬性
7.1 只讀的計(jì)算屬性

 
 
 
  1.  
  2.  
  3.  

7.2 可讀可寫的計(jì)算屬性

 
 
 
  1.  
  2.  
  3.  

8. watch
watch() 函數(shù)用來監(jiān)視某些數(shù)據(jù)項(xiàng)的變化,從而觸發(fā)某些特定的操作,使用之前需要按需導(dǎo)入:

 
 
 
  1. import { watch } from '@vue/composition-api' 

8.1 基本用法

 
 
 
  1.  
  2.  
  3.  

8.2 監(jiān)視數(shù)據(jù)源
監(jiān)視 reactive 類型的數(shù)據(jù)源:

 
 
 
  1.  
  2.  
  3.  

監(jiān)視 ref 類型的數(shù)據(jù)源:

 
 
 
  1. export default { 
  2.   setup() { 
  3.     // 定義數(shù)據(jù)源 
  4.     let count = ref(0); 
  5.     // 指定要監(jiān)視的數(shù)據(jù)源 
  6.     watch(count, (count, prevCount) => { 
  7.       console.log(count, prevCount) 
  8.     }) 
  9.     setInterval(() => { 
  10.       count.value += 2 
  11.     }, 2000) 
  12.     console.log(count.value) 
  13.     return { 
  14.       count 
  15.     } 
  16.   } 
  17. }; 

8.3 監(jiān)聽多個(gè)數(shù)據(jù)源
監(jiān)視 reactive 類型的數(shù)據(jù)源:

 
 
 
  1. export default { 
  2.   setup() { 
  3.     const state = reactive({count: 100, name: 'houfei'}) 
  4.     watch( 
  5.       // 監(jiān)聽count name 
  6.       [() => state.count, () => state.name], 
  7.       // 如果變換 執(zhí)行以下函數(shù) 
  8.       ([newCount, newName], [oldCount, oldName]) => { 
  9.         console.log(newCount, oldCount) 
  10.         console.log(newName, oldName) 
  11.       }, 
  12.       { lazy: true} // 在 watch 被創(chuàng)建的時(shí)候,不執(zhí)行回調(diào)函數(shù)中的代碼 
  13.     ) 
  14.     setTimeout(() => { 
  15.       state.count += 2 
  16.       state.name = 'qweqweewq' 
  17.     }, 3000) 
  18.     return state 
  19.   } 
  20. }; 

監(jiān)視 ref 類型的數(shù)據(jù)源:

 
 
 
  1. export default { 
  2.   setup() { 
  3.     // 定義數(shù)據(jù)源 
  4.     const count = ref(10) 
  5.     const name = ref('zs') 
  6.     // 指定要監(jiān)視的數(shù)據(jù)源 
  7.     watch( 
  8.       [count, name], 
  9.       ([newCount, newName], [oldCount, oldName]) => { 
  10.         console.log(newCount, oldCount) 
  11.         console.log(newName, oldName) 
  12.       }, 
  13.       { lazy: true} 
  14.     ) 
  15.     setInterval(() => { 
  16.       count.value += 2 
  17.     }, 2000) 
  18.     console.log(count.value) 
  19.     return { 
  20.       count 
  21.     } 
  22.   } 
  23. }; 

8.4 清除監(jiān)視
在 setup() 函數(shù)內(nèi)創(chuàng)建的 watch 監(jiān)視,會(huì)在當(dāng)前組件被銷毀的時(shí)候自動(dòng)停止。如果想要明確地停止某個(gè)監(jiān)視,可以調(diào)用 watch() 函數(shù)的返回值即可,語法如下:

 
 
 
  1.  

8.5 在watch中清除無效的異步任務(wù)
有時(shí)候,當(dāng)被 watch 監(jiān)視的值發(fā)生變化時(shí),或 watch 本身被 stop 之后,我們期望能夠清除那些無效的異步任務(wù),此時(shí),watch 回調(diào)函數(shù)中提供了一個(gè) cleanup registrator function 來執(zhí)行清除的工作。這個(gè)清除函數(shù)會(huì)在如下情況下被調(diào)用:

 
 
 
  1. watch 被重復(fù)執(zhí)行了 

 
 
 
  1. watch 被強(qiáng)制 stop 了 

Template 中的代碼示例如下:

 
 
 
  1.  

Script 中的代碼示例如下:

 
 
 
  1.  

9. provide & inject 組件傳值
provide() 和 inject() 可以實(shí)現(xiàn)嵌套組件之間的數(shù)據(jù)傳遞。這兩個(gè)函數(shù)只能在 setup() 函數(shù)中使用。父級組件中使用 provide() 函數(shù)向下傳遞數(shù)據(jù);子級組件中使用 inject() 獲取上層傳遞過來的數(shù)據(jù)。

9.1 共享普通數(shù)據(jù)
app.vue 根組件:

 
 
 
  1.  
  2.  
  3.  

06.son.vue son 組件:

 
 
 
  1.  
  2.  
  3.  

07.grandson.vue son 組件:

 
 
 
  1.  
  2.  
  3.  

9.2 共享ref響應(yīng)式數(shù)據(jù)
app.vue 根組件:

 
 
 
  1.  
  2.  
  3.  

06.son.vue son 組件:

 
 
 
  1.  
  2.  
  3.  

07.grandson.vue son 組件:

 
 
 
  1. template> 
  2.   
     
  3.     grandson 組件 
  4.   
 
  •  
  •  
  •  
  • 10. 節(jié)點(diǎn)的引用 template ref
    10.1 dom的引用

     
     
     
    1.  
    2.  
    3.  

    10.2 組件的引用
    App父組件:

     
     
     
    1.  
    2.  
    3.  

    06.son.vue子組件:

     
     
     
    1.  
    2.  
    3.  

    11 nextTick

     
     
     
    1.  
    2.  
    3.  

    當(dāng)前題目:一篇文章上手Vue3中新增的API
    URL標(biāo)題:http://m.5511xx.com/article/dpijidj.html