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

VUE?mixin?使用示例詳解

 更新時(shí)間:2022年08月11日 09:07:00   作者:余生皆假期-  
混入mixin提供了一種非常靈活的方式,來分發(fā)Vue組件中的可復(fù)用功能,一個(gè)混入對(duì)象可以包含任意組件選項(xiàng),接下來通過本文給大家介紹VUE?mixin?使用,需要的朋友可以參考下

mixin 混入

<!DOCTYPE html>
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>hello world</title>
    <script src="https://unpkg.com/vue@next"></script>
</head>

<body>
    <div id="root"></div>
</body>
<script>
    const MyMixin = {
        data() {
            return {
                number: 2,
                count: 3
            }
        }
    }
    const app = Vue.createApp({
        data() {
            return {
                number: 1
            }
        },
        mixins: [MyMixin],
        template: `
        <div>number:{
<!-- -->{number}}</div>
        <div>count:{
<!-- -->{count}}</div>
        `
    });

    app.mount('#root'); 
</script>

</html>

mixin 混入可以在組件內(nèi)部缺少數(shù)據(jù)時(shí),使用mixin內(nèi)的數(shù)據(jù)代替。

組件 data 優(yōu)先級(jí)高于 mixin data 優(yōu)先級(jí)

上述代碼中,count 使用了 mixin 內(nèi)的數(shù)據(jù),由于內(nèi)部 number 已經(jīng)被定義,vue 優(yōu)先使用內(nèi)部的數(shù)據(jù),再使用 mixin 的數(shù)據(jù)。

2 mixin 生命周期優(yōu)先級(jí)

<!DOCTYPE html>

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>hello world</title>
    <script src="https://cdn.staticfile.org/vue/3.0.5/vue.global.js"></script>
</head>

<body>
    <div id="root"></div>
</body>
<script>
    const MyMixin = {
        created(){
            console.log('mixin created')
        }
    }
    const app = Vue.createApp({
        mixins:[MyMixin],
        created(){
            console.log('app created')
        }
    });

    app.mount('#root'); 
</script>
</html>

mixin 中的生命周期函數(shù)和組件的生命周期函數(shù)都會(huì)執(zhí)行,而且 mixin 中的優(yōu)先級(jí)更高。

效果如下:

3 局部 mixin 和全局 mixin

<!DOCTYPE html>
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>hello world</title>
    <script src="https://cdn.staticfile.org/vue/3.0.5/vue.global.js"></script>
</head>

<body>
    <div id="root"></div>
</body>
<script>
    const MyMixin = {
        data(){
            return{
                number:123
            }
        }
    }
    const app = Vue.createApp({
        mixins:[MyMixin],
        template:`<app/>`
    });

    app.component("app",{
        template:"<div>number:{
<!-- -->{number}}</div>"
    })
    app.mount('#root'); 
</script>

</html>

使用 mixins:[myMixin] 是局部載入mixin的方式,子組件不能獲得 mixins 的值

運(yùn)行結(jié)果如下:

全局 mixin 寫法:

<!DOCTYPE html>
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>hello world</title>
    <script src="https://cdn.staticfile.org/vue/3.0.5/vue.global.js"></script>
</head>

<body>
    <div id="root"></div>
</body>
<script>

    const app = Vue.createApp({
        template: `<app/>`
    });

    app.mixin({
        data() {
            return {
                number: 123
            }
        }
    })

    app.component("app", {
        template: "<div>number:{
<!-- -->{number}}</div>"
    })
    app.mount('#root'); 
</script>
</html>

使用 app.mixin 掛載就是全局,組件可以自由使用。

效果如下:

4 自定義屬性的優(yōu)先級(jí)

<!DOCTYPE html>
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>hello world</title>
    <script src="https://cdn.staticfile.org/vue/3.0.5/vue.global.js"></script>
</head>

<body>
    <div id="root"></div>
</body>
<script>

    const myMixin = {
        value: 1
    }

    const app = Vue.createApp({
        mixins: [myMixin],
        value: 25,
        template: `<div>{
<!-- -->{this.$options.value}}</div>`
    });
    app.mount('#root'); 
</script>

</html>

vue 中,自定義屬性就是直接寫到vue下的屬性,使用 this.$options.value 即可訪問。

自定義屬性組件的優(yōu)先級(jí)比 mixin 優(yōu)先級(jí)高。

結(jié)果如下:

mixin總結(jié):

組件 data,methods優(yōu)先級(jí)高于 mixin data,methods 優(yōu)先級(jí)

生命周期函數(shù),先執(zhí)行 mixin 里邊的,再執(zhí)行組件里邊的

自定義的屬性,組件中的優(yōu)先級(jí)高于 mixin 屬性的優(yōu)先級(jí)。

到此這篇關(guān)于VUE mixin 使用的文章就介紹到這了,更多相關(guān)VUE mixin 使用內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論