<kbd id="5sdj3"></kbd>
<th id="5sdj3"></th>

  • <dd id="5sdj3"><form id="5sdj3"></form></dd>
    <td id="5sdj3"><form id="5sdj3"><big id="5sdj3"></big></form></td><del id="5sdj3"></del>

  • <dd id="5sdj3"></dd>
    <dfn id="5sdj3"></dfn>
  • <th id="5sdj3"></th>
    <tfoot id="5sdj3"><menuitem id="5sdj3"></menuitem></tfoot>

  • <td id="5sdj3"><form id="5sdj3"><menu id="5sdj3"></menu></form></td>
  • <kbd id="5sdj3"><form id="5sdj3"></form></kbd>

    【Vuejs】738- 一篇文章上手Vue3中新增的API

    共 5488字,需瀏覽 11分鐘

     ·

    2020-10-07 05:51


    作者:?liulongbin
    http://www.liulongbin.top:8085/#/?id=_3-setup

    1. 初始化項目

    //?①?npm?i?-g?@vue/cli
    //?②?vue?create?my-project
    //?③?npm?install?@vue/composition-api?-S

    //?④?main,js
    import?Vue?from?'vue'
    import?VueCompositionApi?from?'@vue/composition-api'
    Vue.use(VueCompositionApi)

    2. setup方法

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

    2.1 執(zhí)行時機

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

    setup(props,?ctx)?{
    ????console.log('setup')
    ??},
    ??beforeCreate()?{
    ????console.log('beforeCreate')
    ??},
    ??created()?{
    ????console.log('created')
    ??},

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

    "傳值給?com-setup"/>

    //?通過 setup 函數(shù)的第一個形參,接收 props 數(shù)據(jù):
    setup(props)?{
    ??console.log(props)
    },
    //?在 props 中定義當前組件允許外界傳遞過來的參數(shù)名稱:
    props:?{
    ????p1:?String
    }
    /*
    {}
    p1:?"傳值給?com-setup"
    get?p1:???reactiveGetter()
    set?p1:???reactiveSetter(newVal)
    __proto__:?Object
    */

    2.3 context

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

    setup(props,?ctx)?{
    ????console.log(ctx)
    ????console.log(this)?//?undefined
    ??},
    /*
    attrs:?Object
    emit:???()
    listeners:?Object
    parent:?VueComponent
    refs:?Object
    root:?Vue
    ...
    */

    注意:在 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ù)對象,基本代碼示例如下:





    4. ref

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





    4.1 在 reactive 對象中訪問 ref 創(chuàng)建的響應式數(shù)據(jù)

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

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

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

    setup()?{
    ??//?創(chuàng)建?ref?并掛載到?reactive?中
    ??const?c1?=?ref(0);
    ??const?state?=?reactive({?c1?});

    ??//?再次創(chuàng)建?ref,命名為?c2
    ??const?c2?=?ref(9);
    ??//?將?舊?ref?c1?替換為?新?ref?c2
    ??state.c1?=?c2;
    ??state.c1++;

    ??console.log(state.c1);?//?輸出?10
    ??console.log(c2.value);?//?輸出?10
    ??console.log(c1.value);?//?輸出?0
    }

    5. isRef

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

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

    6. toRefs

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





    7. computed計算屬性

    7.1 只讀的計算屬性




    7.2 可讀可寫的計算屬性




    8. watch

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

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

    8.1 基本用法




    8.2 監(jiān)視數(shù)據(jù)源

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





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

    export?default?{
    ??setup()?{
    ????//?定義數(shù)據(jù)源
    ????let?count?=?ref(0);
    ????//?指定要監(jiān)視的數(shù)據(jù)源
    ????watch(count,?(count,?prevCount)?=>?{
    ??????console.log(count,?prevCount)
    ????})
    ????setInterval(()?=>?{
    ??????count.value?+=?2
    ????},?2000)
    ????console.log(count.value)
    ????return?{
    ??????count
    ????}
    ??}
    };

    8.3 監(jiān)聽多個數(shù)據(jù)源

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

    export?default?{
    ??setup()?{
    ????const?state?=?reactive({count:?100,?name:?'houfei'})
    ????watch(
    ??????//?監(jiān)聽count?name
    ??????[()?=>?state.count,?()?=>?state.name],
    ??????//?如果變換?執(zhí)行以下函數(shù)
    ??????([newCount,?newName],?[oldCount,?oldName])?=>?{
    ????????console.log(newCount,?oldCount)
    ????????console.log(newName,?oldName)
    ??????},
    ??????{?lazy:?true}?//?在?watch?被創(chuàng)建的時候,不執(zhí)行回調函數(shù)中的代碼
    ????)
    ????setTimeout(()?=>?{
    ??????state.count?+=?2
    ??????state.name?=?'qweqweewq'
    ????},?3000)
    ????return?state
    ??}
    };

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

    export?default?{
    ??setup()?{
    ????//?定義數(shù)據(jù)源
    ????const?count?=?ref(10)
    ????const?name?=?ref('zs')
    ????//?指定要監(jiān)視的數(shù)據(jù)源
    ????watch(
    ??????[count,?name],
    ??????([newCount,?newName],?[oldCount,?oldName])?=>?{
    ????????console.log(newCount,?oldCount)
    ????????console.log(newName,?oldName)
    ??????},
    ??????{?lazy:?true}
    ????)
    ????setInterval(()?=>?{
    ??????count.value?+=?2
    ????},?2000)
    ????console.log(count.value)
    ????return?{
    ??????count
    ????}
    ??}
    };

    8.4 清除監(jiān)視

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



    8.5 在watch中清除無效的異步任務

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

    watch 被重復執(zhí)行了

    watch 被強制 stop 了

    Template 中的代碼示例如下:



    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 根組件:






    06.son.vue son 組件:





    07.grandson.vue son 組件:





    9.2 共享ref響應式數(shù)據(jù)

    app.vue 根組件:





    06.son.vue son 組件:





    07.grandson.vue son 組件:

    template>
    ??

    ????"{color:?color}">grandson?組件
    ??





    10. 節(jié)點的引用 template ref

    10.1 dom的引用




    10.2 組件的引用

    App父組件:





    06.son.vue子組件:





    11 nextTick






    最近發(fā)現(xiàn)一個好網(wǎng)站,不需要梯子就能看Google Cloud Next ’20大會的視頻和技術干貨,速戳:https://sourl.cn/AnzYaG

    1. JavaScript 重溫系列(22篇全)
    2. ECMAScript 重溫系列(10篇全)
    3. JavaScript設計模式 重溫系列(9篇全)
    4.?正則 / 框架 / 算法等 重溫系列(16篇全)
    5.?Webpack4 入門(上)||?Webpack4 入門(下)
    6.?MobX 入門(上)?||??MobX 入門(下)
    7. 80+篇原創(chuàng)系列匯總

    回復“加群”與大佬們一起交流學習~

    點擊“閱讀原文”查看 80+ 篇原創(chuàng)文章

    瀏覽 55
    點贊
    評論
    收藏
    分享

    手機掃一掃分享

    分享
    舉報
    評論
    圖片
    表情
    推薦
    點贊
    評論
    收藏
    分享

    手機掃一掃分享

    分享
    舉報

    <kbd id="5sdj3"></kbd>
    <th id="5sdj3"></th>

  • <dd id="5sdj3"><form id="5sdj3"></form></dd>
    <td id="5sdj3"><form id="5sdj3"><big id="5sdj3"></big></form></td><del id="5sdj3"></del>

  • <dd id="5sdj3"></dd>
    <dfn id="5sdj3"></dfn>
  • <th id="5sdj3"></th>
    <tfoot id="5sdj3"><menuitem id="5sdj3"></menuitem></tfoot>

  • <td id="5sdj3"><form id="5sdj3"><menu id="5sdj3"></menu></form></td>
  • <kbd id="5sdj3"><form id="5sdj3"></form></kbd>
    免费在线观看波多野结衣av | 久草久| 特级西西人体4444XXXX | 尻屄尻美女屄屄网 | 日韩色情无码 |