成人免费xxxxx在线视频软件_久久精品久久久_亚洲国产精品久久久_天天色天天色_亚洲人成一区_欧美一级欧美三级在线观看

一篇文章上手Vue3中新增的API

開(kāi)發(fā)
今天介紹一篇文章上手Vue3中新增的API

[[344491]]

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

  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. <com-setup p1="傳值給 com-setup"/> 

  1. // 通過(guò) setup 函數(shù)的第一個(gè)形參,接收 props 數(shù)據(jù): 
  2. setup(props) { 
  3.   console.log(props) 
  4. }, 
  5. // 在 props 中定義當(dāng)前組件允許外界傳遞過(guò)來(lái)的參數(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è)上下文對(duì)象,這個(gè)上下文對(duì)象中包含了一些有用的屬性,這些屬性在 vue 2.x 中需要通過(guò) this 才能訪問(wèn)到,在 vue 3.x 中,它們的訪問(wèn)方式如下:

  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ù)中無(wú)法訪問(wèn)到 this

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

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

  1. <template> 
  2.   <div> 
  3.     <!-- 在 template 中訪問(wèn)響應(yīng)式數(shù)據(jù) --> 
  4.     <p>當(dāng)前的 count 值為:{{count}}</p> 
  5.     <button @click="count += 1">+1</button> 
  6.   </div> 
  7. </template> 
  8.  
  9. <script> 
  10. import {reactive} from '@vue/composition-api' 
  11. export default { 
  12.   setup(props, ctx) { 
  13.     // 創(chuàng)建響應(yīng)式數(shù)據(jù)對(duì)象,得到的 state 類似于 vue 2.x 中 data() 返回的響應(yīng)式對(duì)象 
  14.     const state = reactive({ count: 0 }) 
  15.     state.count += 1 
  16.     console.log(state) 
  17.      // setup 函數(shù)中將響應(yīng)式數(shù)據(jù)對(duì)象 return 出去,供 template 使用 
  18.     return state 
  19.   } 
  20. </script> 

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

  1. <template> 
  2.   <div> 
  3.     <h3>02.ref.vue 文件</h3> 
  4.     <p>refCount:{{refCount}}</p> 
  5.     <button @click="refCount += 1">+1</button> 
  6.   </div> 
  7. </template> 
  8.  
  9. <script> 
  10. import { ref } from '@vue/composition-api' 
  11. export default { 
  12.   setup() { 
  13.     // / 創(chuàng)建響應(yīng)式數(shù)據(jù)對(duì)象 count,初始值為 0 
  14.     const refCount = ref(0) 
  15.     // 如果要訪問(wèn) ref() 創(chuàng)建出來(lái)的響應(yīng)式數(shù)據(jù)對(duì)象的值,必須通過(guò) .value 屬性才可以,只有在setup內(nèi)部才需要 .value 屬性 
  16.     console.log(refCount.value) // 輸出 0 
  17.     // 讓 refCount 的值 +1 
  18.         refCount.value++ 
  19.     // 再次打印 refCount 的值 
  20.         console.log(refCount.value) // 輸出 1 
  21.     return { 
  22.       refCount 
  23.     } 
  24.   } 
  25. </script> 

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

  1. setup() { 
  2.   const refCount = ref(0) 
  3.   const state = reactive({refCount}) 
  4.   console.log(state.refCount) // 輸出 0 
  5.   state.refCount++ // 此處不需要通過(guò) .value 就能直接訪問(wèn)原始值 
  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() 用來(lái)判斷某個(gè)值是否為 ref() 創(chuàng)建出來(lái)的對(duì)象;應(yīng)用場(chǎng)景:當(dāng)需要展開(kāi)某個(gè)可能為 ref() 創(chuàng)建出來(lái)的值的時(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)建出來(lái)的響應(yīng)式對(duì)象,轉(zhuǎn)換為普通的對(duì)象,只不過(guò),這個(gè)對(duì)象上的每個(gè)屬性節(jié)點(diǎn),都是 ref() 類型的響應(yīng)式數(shù)據(jù)。

  1. <template> 
  2.   <div> 
  3.     <h3>03.toRefs.vue文件</h3> 
  4.     <p>{{ count }} - {{ name }}</p> 
  5.     <button @click="count += 1">+1</button> 
  6.     <button @click="add">+1</button> 
  7.   </div> 
  8. </template> 
  9.  
  10. <script> 
  11. import { reactive, toRefs } from "@vue/composition-api"
  12. export default { 
  13.   setup() { 
  14.     // 響應(yīng)式數(shù)據(jù) 
  15.     const state = reactive({ count: 0, name"zs" }); 
  16.     // 方法 
  17.     const add = () => { 
  18.       state.count += 1; 
  19.     }; 
  20.     return { 
  21.       // 非響應(yīng)式數(shù)據(jù) 
  22.       // ...state, 
  23.       // 響應(yīng)式數(shù)據(jù) 
  24.       ...toRefs(state), 
  25.       add 
  26.     }; 
  27.   } 
  28. }; 
  29. </script> 

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

  1. <template> 
  2.   <div> 
  3.     <h3>04.computed.vue文件</h3> 
  4.     <p>refCount: {{refCount}}</p> 
  5.     <p>計(jì)算屬性的值computedCount : {{computedCount}}</p> 
  6.     <button @click="refCount++">refCount + 1</button> 
  7.         <!-- 點(diǎn)擊報(bào)錯(cuò) --> 
  8.     <button @click="computedCount++">計(jì)算屬性的值computedCount + 1</button> 
  9.   </div> 
  10. </template> 
  11.  
  12. <script> 
  13. import { computed, ref } from '@vue/composition-api' 
  14. export default { 
  15.   setup() { 
  16.     const refCount = ref(1) 
  17.     // 只讀 
  18.     let computedCount = computed(() => refCount.value + 1) 
  19.     console.log(computedCount) 
  20.     return { 
  21.       refCount, 
  22.       computedCount 
  23.     } 
  24.   } 
  25. }; 
  26. </script> 

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

  1. <template> 
  2.   <div> 
  3.     <h3>04.computed.vue文件</h3> 
  4.     <p>refCount: {{refCount}}</p> 
  5.     <p>計(jì)算屬性的值computedCount : {{computedCount}}</p> 
  6.     <button @click="refCount++">refCount + 1</button> 
  7.   </div> 
  8. </template> 
  9.  
  10. <script> 
  11. import { computed, ref } from '@vue/composition-api' 
  12. export default { 
  13.   setup() { 
  14.     const refCount = ref(1) 
  15.     // 可讀可寫(xiě) 
  16.     let computedCount = computed({ 
  17.       // 取值函數(shù) 
  18.       get: () => refCount.value + 1, 
  19.       // 賦值函數(shù) 
  20.       set: val => { 
  21.         refCount.value = refCount.value -5 
  22.       } 
  23.     }) 
  24.     console.log(computedCount.value) 
  25.     // 為計(jì)算屬性賦值的操作,會(huì)觸發(fā) set 函數(shù) 
  26.     computedCount.value = 10 
  27.     console.log(computedCount.value) 
  28.     // 觸發(fā) set 函數(shù)后,count 的值會(huì)被更新 
  29.     console.log(refCount.value) 
  30.     return { 
  31.       refCount, 
  32.       computedCount 
  33.     } 
  34.   } 
  35. }; 
  36. </script> 

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

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

8.1 基本用法

  1. <template> 
  2.   <div> 
  3.     <h3>05.watch.vue文件</h3> 
  4.     <p>refCount: {{refCount}}</p> 
  5.   </div> 
  6. </template> 
  7.  
  8. <script> 
  9. import { watch, ref } from '@vue/composition-api' 
  10. export default { 
  11.   setup() { 
  12.     const refCount = ref(100) 
  13.     // 定義 watch,只要 count 值變化,就會(huì)觸發(fā) watch 回調(diào) 
  14.     // 組件在第一次創(chuàng)建的時(shí)候執(zhí)行一次 watch 
  15.     watch(() => console.log(refCount.value), { lazy: false}) 
  16.     setInterval(() => { 
  17.       refCount.value += 2 
  18.     }, 5000) 
  19.     return { 
  20.       refCount 
  21.     } 
  22.   } 
  23. }; 
  24. </script> 

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

  1. <template> 
  2.   <div> 
  3.     <h3>05.watch.vue文件</h3> 
  4.     <p>count: {{count}}</p> // 不是響應(yīng)式數(shù)據(jù) 
  5.   </div> 
  6. </template> 
  7.  
  8. <script> 
  9. import { watch, ref, reactive } from '@vue/composition-api' 
  10. export default { 
  11.   setup() { 
  12.     const state = reactive({count: 100}) 
  13.     watch( 
  14.       // 監(jiān)聽(tīng)count 
  15.       () => state.count
  16.       // 如果變換 執(zhí)行以下函數(shù) 
  17.       (newVal, oldVala) => { 
  18.         console.log(newVal, oldVala) 
  19.       }, 
  20.       { lazy: true } 
  21.     ) 
  22.     setInterval(() => { 
  23.       state.count += 2 
  24.     }, 5000) 
  25.     return state 
  26.   } 
  27. }; 
  28. </script> 

監(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)聽(tīng)多個(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)聽(tīng)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.       [countname], 
  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ù)的返回值即可,語(yǔ)法如下:

  1. <script> 
  2. // 創(chuàng)建監(jiān)視,并得到 停止函數(shù) 
  3. const stop = watch(() => { 
  4.   /* ... */ 
  5. }) 
  6.  
  7. // 調(diào)用停止函數(shù),清除對(duì)應(yīng)的監(jiān)視 
  8. stop() 

  1. <template> 
  2.   <div> 
  3.     <!-- <h3>05.watch.vue文件</h3> --> 
  4.     <p>count: {{ count }}</p> 
  5.     <button @click="stopWatch">停止監(jiān)聽(tīng)</button> 
  6.   </div> 
  7. </template> 
  8.  
  9. <script> 
  10. import { watch, ref, reactive } from "@vue/composition-api"
  11. export default { 
  12.   setup() { 
  13.     // 定義數(shù)據(jù)源 
  14.     const count = ref(10) 
  15.     const name = ref('zs'
  16.     // 指定要監(jiān)視的數(shù)據(jù)源 
  17.     const stop = watch( 
  18.       [countname], 
  19.       ([newCount, newName], [oldCount, oldName]) => { 
  20.         console.log(newCount, oldCount) 
  21.         console.log(newName, oldName) 
  22.       }, 
  23.       { lazy: true
  24.     ) 
  25.     setInterval(() => { 
  26.       count.value += 2 
  27.       name.value = 'houyue' 
  28.     }, 2000) 
  29.     // 停止監(jiān)視 
  30.     const stopWatch = () => { 
  31.       console.log("停止監(jiān)視,但是數(shù)據(jù)還在變化"
  32.       stop() 
  33.     } 
  34.     console.log(count.value) 
  35.     return { 
  36.       stop, 
  37.       count
  38.       stopWatch 
  39.     } 
  40.   } 
  41. }; 
  42.  
  43. </script> 

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

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

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

Template 中的代碼示例如下:

  1. <template> 
  2.   <div> 
  3.     <!-- <h3>05.watch.vue文件</h3> --> 
  4.     <input type="text" v-model="keywords" /> 
  5.     <p>keywords:--- {{ keywords }}</p> 
  6.   </div> 
  7. </template> 

Script 中的代碼示例如下:

  1. <script> 
  2. import { watch, ref, reactive } from "@vue/composition-api"
  3.  
  4. export default { 
  5.   setup() { 
  6.     // 定義響應(yīng)式數(shù)據(jù) keywords 
  7.     const keywords = ref(""); 
  8.  
  9.     // 異步任務(wù):打印用戶輸入的關(guān)鍵詞 
  10.     const asyncPrint = val => { 
  11.       // 延時(shí) 1 秒后打印 
  12.       return setTimeout(() => { 
  13.         console.log(val); 
  14.       }, 1000); 
  15.     }; 
  16.  
  17.     // 定義 watch 監(jiān)聽(tīng) 
  18.     watch( 
  19.       keywords, 
  20.       (keywords, prevKeywords, onCleanup) => { 
  21.         // 執(zhí)行異步任務(wù),并得到關(guān)閉異步任務(wù)的 timerId 
  22.         const timerId = asyncPrint(keywords); 
  23.         // 如果 watch 監(jiān)聽(tīng)被重復(fù)執(zhí)行了,則會(huì)先清除上次未完成的異步任務(wù) 
  24.         onCleanup(() => clearTimeout(timerId)); 
  25.       }, 
  26.       // watch 剛被創(chuàng)建的時(shí)候不執(zhí)行 
  27.       { lazy: true } 
  28.     ); 
  29.  
  30.     // 把 template 中需要的數(shù)據(jù) return 出去 
  31.     return { 
  32.       keywords 
  33.     }; 
  34.   } 
  35. }; 
  36. </script> 

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

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

  1. <template> 
  2.   <div id="app"
  3.     <h1>父組件</h1> 
  4.     <button @click="color = 'blue'">藍(lán)色</button> 
  5.     <button @click="color = 'red'">紅色</button> 
  6.     <button @click="color = 'yellow'">黃色</button> 
  7.     <son></son> 
  8.     <son></son> 
  9.   </div> 
  10. </template> 
  11.  
  12. <script> 
  13. import { ref, provide } from '@vue/composition-api' 
  14. import Son from './components/06.son.vue' 
  15.  
  16. export default { 
  17.   name'app'
  18.   components: { 
  19.     'son': Son 
  20.   }, 
  21.   setup() { 
  22.     const color = ref('green'
  23.     provide('themecolor', color) 
  24.     return { 
  25.      color 
  26.     } 
  27.   } 
  28. </script> 

06.son.vue son 組件:

  1. <template> 
  2.   <div> 
  3.     <h3 :style="{color: color}">son 組件</h3> 
  4.     <grandson></grandson> 
  5.   </div> 
  6. </template> 
  7.  
  8. <script> 
  9. import { inject } from '@vue/composition-api' 
  10. import Grandson from './07.grandson.vue' 
  11. export default { 
  12.     components: { 
  13.     'grandson': Grandson 
  14.   }, 
  15.   setup() { 
  16.     const color = inject('themecolor'
  17.     return { 
  18.      color 
  19.     } 
  20.   } 
  21. </script> 

07.grandson.vue son 組件:

  1. <template> 
  2.   <div> 
  3.     <h5 :style="{color: color}">grandson 組件</h5> 
  4.   </div> 
  5. </template> 
  6.  
  7. <script> 
  8. import { inject } from '@vue/composition-api' 
  9. export default { 
  10.   setup() { 
  11.     const color = inject('themecolor'
  12.     return { 
  13.       color 
  14.     } 
  15.   } 
  16. </script> 

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

  1. <template> 
  2.   <div id="app"
  3.     <h1>父組件</h1> 
  4.     <son></son> 
  5.   </div> 
  6. </template> 
  7.  
  8. <script> 
  9. import { provide } from '@vue/composition-api' 
  10. import Son from './components/06.son.vue' 
  11.  
  12. export default { 
  13.   name'app'
  14.   components: { 
  15.     'son': Son 
  16.   }, 
  17.   setup() { 
  18.     provide('themecolor''red'
  19.   } 
  20. </script> 

06.son.vue son 組件:

  1. <template> 
  2.   <div> 
  3.     <h3 :style="{color: color}">son 組件</h3> 
  4.     <grandson></grandson> 
  5.   </div> 
  6. </template> 
  7.  
  8. <script> 
  9. import { inject } from '@vue/composition-api' 
  10. import Grandson from './07.grandson.vue' 
  11. export default { 
  12.     components: { 
  13.     'grandson': Grandson 
  14.   }, 
  15.   setup() { 
  16.     const color = inject('themecolor'
  17.     return { 
  18.       color 
  19.     } 
  20.   } 
  21. </script> 

07.grandson.vue son 組件:

  1. template> 
  2.   <div> 
  3.     <h5 :style="{color: color}">grandson 組件</h5> 
  4.   </div> 
  5. </template> 
  6.  
  7. <script> 
  8. import { inject } from '@vue/composition-api' 
  9. export default { 
  10.   setup() { 
  11.     const color = inject('themecolor'
  12.     return { 
  13.       color 
  14.     } 
  15.   } 
  16. </script> 

10. 節(jié)點(diǎn)的引用 template ref
10.1 dom的引用

  1. <template> 
  2.   <div> 
  3.     <h3 ref="h3Ref">TemplateRefOne</h3> 
  4.   </div> 
  5. </template> 
  6.  
  7. <script> 
  8. import { ref, onMounted } from '@vue/composition-api' 
  9.  
  10. export default { 
  11.   setup() { 
  12.     // 創(chuàng)建一個(gè) DOM 引用 
  13.     const h3Ref = ref(null
  14.  
  15.     // 在 DOM 首次加載完畢之后,才能獲取到元素的引用 
  16.     onMounted(() => { 
  17.       // 為 dom 元素設(shè)置字體顏色 
  18.       // h3Ref.value 是原生DOM對(duì)象 
  19.       h3Ref.value.style.color = 'red' 
  20.     }) 
  21.  
  22.     // 把創(chuàng)建的引用 return 出去 
  23.     return { 
  24.       h3Ref 
  25.     } 
  26.   } 
  27. </script> 

10.2 組件的引用
App父組件:

  1. <template> 
  2.   <div id="app"
  3.     <h1>父組件</h1> 
  4.     <button @click="showComRef">展示子組件的值</button> 
  5.     <son ref="comRef"></son> 
  6.   </div> 
  7. </template> 
  8.  
  9. <script> 
  10.  
  11. import Son from './components/06.son.vue' 
  12.  
  13. export default { 
  14.   name'app'
  15.   components: { 
  16.     'son': Son 
  17.   }, 
  18.   setup() { 
  19.     const comRef = ref(null)  
  20.     const showComRef = () => { 
  21.       console.log(comRef) 
  22.       console.log('str1的值是' + comRef.value.str1) 
  23.       comRef.value.setStr1() 
  24.     } 
  25.     return { 
  26.       comRef, 
  27.       showComRef 
  28.     } 
  29.   } 
  30. </script> 

06.son.vue子組件:

  1. <template> 
  2.   <div> 
  3.     <h3 :style="{color: color}">son 組件</h3> 
  4.     <p>{{str1}}</p> 
  5.   </div> 
  6. </template> 
  7.  
  8. <script> 
  9. import { ref } from '@vue/composition-api' 
  10. export default { 
  11.   setup() { 
  12.     const str1 = ref('這是一個(gè)子組件!!'
  13.     const setStr1 = () => { 
  14.       str1.value = '被賦值了' 
  15.     } 
  16.     return { 
  17.       str1, 
  18.       setStr1 
  19.     } 
  20.   } 
  21. </script> 

11 nextTick

  1. <template> 
  2.   <div> 
  3.     <h3>09.nextTick 組件</h3> 
  4.     <p>學(xué)習(xí) $nextTick</p> 
  5.     <button v-if="isShowInput === false" @click="showInput">展示文本框</button> 
  6.     <input type="text" v-else ref="ipt"
  7.   </div> 
  8. </template> 
  9.  
  10. <script> 
  11. export default { 
  12.   data() { 
  13.     return { 
  14.       isShowInput: false 
  15.     } 
  16.   }, 
  17.   methods: { 
  18.     showInput() { 
  19.       this.isShowInput = !this.isShowInput 
  20.       // console.log(this.$refs) 
  21.       this.$nextTick(() => { 
  22.         this.$refs.ipt.focus() 
  23.       }) 
  24.     } 
  25.   } 
  26. </script> 

 

 

責(zé)任編輯:姜華 來(lái)源: 前端工匠
相關(guān)推薦

2022-05-28 15:59:55

PythonPandas數(shù)據(jù)可視化

2020-10-09 08:15:11

JsBridge

2023-04-13 08:21:38

DevOpsAPI管理平臺(tái)

2021-05-18 09:00:28

Pythonclass

2021-08-12 14:19:14

Slice數(shù)組類型內(nèi)存

2022-02-21 09:44:45

Git開(kāi)源分布式

2019-04-17 15:16:00

Sparkshuffle算法

2024-06-25 08:18:55

2021-06-30 00:20:12

Hangfire.NET平臺(tái)

2023-05-12 08:19:12

Netty程序框架

2021-04-09 08:40:51

網(wǎng)絡(luò)保險(xiǎn)網(wǎng)絡(luò)安全網(wǎng)絡(luò)風(fēng)險(xiǎn)

2017-09-05 08:52:37

Git程序員命令

2020-10-23 07:56:04

Java中的IO流

2022-01-15 10:02:03

Java Hashtable類 Java 基礎(chǔ)

2021-11-10 09:19:41

PythonShutil模塊

2021-11-17 10:11:08

PythonLogging模塊

2011-07-12 13:35:04

程序員

2020-10-22 08:25:22

JavaScript運(yùn)作原理

2019-01-09 10:04:16

2020-02-28 11:29:00

ElasticSear概念類比
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)

主站蜘蛛池模板: 91久久国产综合久久91精品网站 | 九九国产在线观看 | 91在线精品秘密一区二区 | 夜夜爽99久久国产综合精品女不卡 | 欧美日韩成人在线观看 | 国产一区二区在线免费视频 | 久久久久免费精品国产小说色大师 | 香蕉久久久 | 亚洲成av | 黄色大片免费网站 | 中文字幕av一区二区三区 | 久久这里只有精品首页 | 日本黄色影片在线观看 | 99久久久久 | 日韩一区二区三区在线观看视频 | 日韩欧美二区 | 欧美综合视频在线 | 免费观看的av | 一区精品国产欧美在线 | 精品av天堂毛片久久久借种 | 一区二区三区中文字幕 | 成人久久久 | 成人福利视频网站 | 欧美日韩国产一区二区三区不卡 | 美女一区二区在线观看 | 亚洲欧美日韩网站 | 天天色综网 | 理论片87福利理论电影 | 在线观看av网站 | 亚洲国产精品一区二区第一页 | 一级片片| 一区二区三区四区在线 | 婷婷福利视频导航 | 亚洲欧美日韩中文字幕一区二区三区 | 亚洲激情在线视频 | 日韩免费毛片视频 | 久久aⅴ乱码一区二区三区 亚洲国产成人精品久久久国产成人一区 | 精品欧美乱码久久久久久 | 日韩 欧美 综合 | 人妖videosex高潮另类 | 国产一二区视频 |