zoukankan      html  css  js  c++  java
  • mixins

    混合 (mixins) 是一种分发 Vue 组件中可复用功能的非常灵活的方式。

    相当于扩充父组件的属性、方法等,类似于在原型对象中注册方法。

    如果在引用mixins的同时,在组件中重复定义相同的方法,则mixins中的方法会被覆盖

    <div id="app">
        <child></child>
        <kid></kid>
    </div>
    
    //不用minins,同样的方法要定义两次
    Vue.component('child',{
        template:`<h1 @click="foo">child component</h1>`,
        methods:{
            foo(){
                console.log('Child foo()'+this.msg++)
            }
        }
    })
     
    Vue.component('kid',{
        template:`<h1 @click="foo">kid component</h1>`,
        methods:{
            foo(){
                console.log('Kid foo()'+this.msg++)
            }
        }
    })
    
    //用minins,抽取公用的方法和属性,实现了功能的复用
    let mixin={
        data(){
            return{
                msg:1
            }
        },
        methods:{
            foo(){
                console.log('hello from mixin!----'+this.msg++)
            }
        }
    }
    var child=Vue.component('child',{ 
            template:`<h1 @click="foo">child component</h1>`, 
            mixins:[mixin]
    })
    Vue.component('kid',{ 
            template:`<h1 @click="foo">kid component</h1>`, 
            mixins:[mixin]
    })
  • 相关阅读:
    文本查询程序再探
    第15章 面向对象程序设计
    错误和异常处理 使用模板
    PHP会话管理
    身份验证
    表单提交与接收 文件提交与接收
    PHP文件访问
    PHP面向对象
    PHP速学
    第14章 重载运算与类型转换
  • 原文地址:https://www.cnblogs.com/annie211/p/12666282.html
Copyright © 2011-2022 走看看