首页 > 其他 > 详细

Vue mixin

时间:2021-08-25 18:28:58      阅读:20      评论:0      收藏:0      [点我收藏+]

 

 

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
    <div id="app1">
        <h1>{{msg}}</h1>
        <h1>{{this.$options.age}}</h1>
    </div>
    <p>----------------------------</p>
    <div id="app2">
        <h1>{{msg}}</h1>
    </div>
    <script src="js/vue.3.2.2.js"></script>
    <script>
        //定义一个mixin对象
        const myMixin = {
            data(){//定义数据
                return {
                    msg:你好!
                }
            },
            methods:{
                hello(){
                    console.log("hello");
                }
            },
            mounted(){
                console.log("mounted");
            },
            age:100
        };
        // 1、创建Vue的实例对象
        const app1 = Vue.createApp({
            data(){//定义数据
                return {
                    msg:你好1!
                }
            },
            methods:{
                hello(){
                    console.log("hello1");
                }
            },
            mixins:[myMixin],
            age:99,
            mounted(){
                console.log("mounted1"+this.$options);
            }
        });
        //通过配置决定使用哪个属性
        app1.config.optionMergeStrategies.age = (mixinVal,appValue)=>{
            return mixinVal || appValue;
        }
        app1.mount(#app1);

        const app2 = Vue.createApp({
            // mixins:[myMixin]
        });
        app2.mixin({
            data(){//定义数据
                return {
                    msg:你好2!
                }
            },
            methods:{
                hello(){
                    console.log("hello");
                }
            },
            mounted(){
                console.log("mounted");
            },
            age:100
        });
        app2.mount(#app2);

    </script>
</body>
</html>

 

Vue mixin

原文:https://www.cnblogs.com/mingforyou/p/15185158.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!