自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

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

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

[[344491]]

1. 初始化項目

  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í)行時機
setup的執(zhí)行時機在: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. // 通過 setup 函數(shù)的第一個形參,接收 props 數(shù)據(jù): 
  2. setup(props) { 
  3.   console.log(props) 
  4. }, 
  5. // 在 props 中定義當前組件允許外界傳遞過來的參數(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ù)的第二個形參是一個上下文對象,這個上下文對象中包含了一些有用的屬性,這些屬性在 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ù)接收一個普通函數(shù),返回一個響應式的數(shù)據(jù)對象。

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

  1. <template> 
  2.   <div> 
  3.     <!-- 在 template 中訪問響應式數(shù)據(jù) --> 
  4.     <p>當前的 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)建響應式數(shù)據(jù)對象,得到的 state 類似于 vue 2.x 中 data() 返回的響應式對象 
  14.     const state = reactive({ count: 0 }) 
  15.     state.count += 1 
  16.     console.log(state) 
  17.      // setup 函數(shù)中將響應式數(shù)據(jù)對象 return 出去,供 template 使用 
  18.     return state 
  19.   } 
  20. </script> 

4. ref
ref() 函數(shù)用來根據(jù)給定的值創(chuàng)建一個響應式的數(shù)據(jù)對象,ref() 函數(shù)調(diào)用的返回值是一個對象,這個對象上只包含一個 .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)建響應式數(shù)據(jù)對象 count,初始值為 0 
  14.     const refCount = ref(0) 
  15.     // 如果要訪問 ref() 創(chuàng)建出來的響應式數(shù)據(jù)對象的值,必須通過 .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 對象中訪問 ref 創(chuàng)建的響應式數(shù)據(jù)
當把 ref() 創(chuàng)建出來的響應式數(shù)據(jù)對象,掛載到 reactive() 上時,會自動把響應式數(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 會覆蓋舊的 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() 用來判斷某個值是否為 ref() 創(chuàng)建出來的對象;應用場景:當需要展開某個可能為 ref() 創(chuàng)建出來的值的時候,例如:

  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)建出來的響應式對象,轉(zhuǎn)換為普通的對象,只不過,這個對象上的每個屬性節(jié)點,都是 ref() 類型的響應式數(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.     // 響應式數(shù)據(jù) 
  15.     const state = reactive({ count: 0, name"zs" }); 
  16.     // 方法 
  17.     const add = () => { 
  18.       state.count += 1; 
  19.     }; 
  20.     return { 
  21.       // 非響應式數(shù)據(jù) 
  22.       // ...state, 
  23.       // 響應式數(shù)據(jù) 
  24.       ...toRefs(state), 
  25.       add 
  26.     }; 
  27.   } 
  28. }; 
  29. </script> 

7. computed計算屬性
7.1 只讀的計算屬性

  1. <template> 
  2.   <div> 
  3.     <h3>04.computed.vue文件</h3> 
  4.     <p>refCount: {{refCount}}</p> 
  5.     <p>計算屬性的值computedCount : {{computedCount}}</p> 
  6.     <button @click="refCount++">refCount + 1</button> 
  7.         <!-- 點擊報錯 --> 
  8.     <button @click="computedCount++">計算屬性的值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 可讀可寫的計算屬性

  1. <template> 
  2.   <div> 
  3.     <h3>04.computed.vue文件</h3> 
  4.     <p>refCount: {{refCount}}</p> 
  5.     <p>計算屬性的值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.     // 可讀可寫 
  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.     // 為計算屬性賦值的操作,會觸發(fā) set 函數(shù) 
  26.     computedCount.value = 10 
  27.     console.log(computedCount.value) 
  28.     // 觸發(fā) set 函數(shù)后,count 的值會被更新 
  29.     console.log(refCount.value) 
  30.     return { 
  31.       refCount, 
  32.       computedCount 
  33.     } 
  34.   } 
  35. }; 
  36. </script> 

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

  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 值變化,就會觸發(fā) watch 回調(diào) 
  14.     // 組件在第一次創(chuàng)建的時候執(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> // 不是響應式數(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)聽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)聽多個數(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)建的時候,不執(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)視,會在當前組件被銷毀的時候自動停止。如果想要明確地停止某個監(jiān)視,可以調(diào)用 watch() 函數(shù)的返回值即可,語法如下:

  1. <script> 
  2. // 創(chuàng)建監(jiān)視,并得到 停止函數(shù) 
  3. const stop = watch(() => { 
  4.   /* ... */ 
  5. }) 
  6.  
  7. // 調(diào)用停止函數(shù),清除對應的監(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)聽</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中清除無效的異步任務
有時候,當被 watch 監(jiān)視的值發(fā)生變化時,或 watch 本身被 stop 之后,我們期望能夠清除那些無效的異步任務,此時,watch 回調(diào)函數(shù)中提供了一個 cleanup registrator function 來執(zhí)行清除的工作。這個清除函數(shù)會在如下情況下被調(diào)用:

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

  1. watch 被強制 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.     // 定義響應式數(shù)據(jù) keywords 
  7.     const keywords = ref(""); 
  8.  
  9.     // 異步任務:打印用戶輸入的關(guān)鍵詞 
  10.     const asyncPrint = val => { 
  11.       // 延時 1 秒后打印 
  12.       return setTimeout(() => { 
  13.         console.log(val); 
  14.       }, 1000); 
  15.     }; 
  16.  
  17.     // 定義 watch 監(jiān)聽 
  18.     watch( 
  19.       keywords, 
  20.       (keywords, prevKeywords, onCleanup) => { 
  21.         // 執(zhí)行異步任務,并得到關(guān)閉異步任務的 timerId 
  22.         const timerId = asyncPrint(keywords); 
  23.         // 如果 watch 監(jiān)聽被重復執(zhí)行了,則會先清除上次未完成的異步任務 
  24.         onCleanup(() => clearTimeout(timerId)); 
  25.       }, 
  26.       // watch 剛被創(chuàng)建的時候不執(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() 可以實現(xiàn)嵌套組件之間的數(shù)據(jù)傳遞。這兩個函數(shù)只能在 setup() 函數(shù)中使用。父級組件中使用 provide() 函數(shù)向下傳遞數(shù)據(jù);子級組件中使用 inject() 獲取上層傳遞過來的數(shù)據(jù)。

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

  1. <template> 
  2.   <div id="app"
  3.     <h1>父組件</h1> 
  4.     <button @click="color = 'blue'">藍色</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響應式數(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é)點的引用 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)建一個 DOM 引用 
  13.     const h3Ref = ref(null
  14.  
  15.     // 在 DOM 首次加載完畢之后,才能獲取到元素的引用 
  16.     onMounted(() => { 
  17.       // 為 dom 元素設置字體顏色 
  18.       // h3Ref.value 是原生DOM對象 
  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('這是一個子組件??!'
  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>學習 $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> 

 

 

責任編輯:姜華 來源: 前端工匠
相關(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管理平臺

2021-05-18 09:00:28

Pythonclass

2021-08-12 14:19:14

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

2022-02-21 09:44:45

Git開源分布式

2023-05-12 08:19:12

Netty程序框架

2021-06-30 00:20:12

Hangfire.NET平臺

2019-04-17 15:16:00

Sparkshuffle算法

2024-06-25 08:18:55

2021-04-09 08:40:51

網(wǎng)絡保險網(wǎng)絡安全網(wǎng)絡風險

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

程序員

2019-10-17 19:15:22

jQueryJavaScript前端

2021-05-15 09:18:04

Python進程

2020-02-28 11:29:00

ElasticSear概念類比
點贊
收藏

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