Vue学习——mixin混入

功能:可以把多个组件共用的配置提取成一个混入对象


使用方式:
    1.定义混合,例如:
        {
        data(){....},
        methods:{....}
        ....
    }
    2.使用混入,例如:
        全局混入:Vue.mixin(xxx)
        局部混入:mixins:['xxx']

Blogger.vue

<template>
    <div class="blogger">
        <h1>{{msg}}</h1>
        <h2 @click="showName">博主尊称:{{name}}</h2>
        <h2>博主性别:{{sex}}</h2>
        <el-button type="primary" icon="el-icon-s-promotion" @click="skip">点我跳转到博主博客</el-button>
    </div>
</template>

<script>
import {mixin} from '../mixin'

export default {
    name:'Blogger',
    data() {
        return {
            msg:'青衫烟雨客,似是故人来',
        }
    },
    props:['name','sex'],
    mixins:[mixin]
}
</script>

<style>
    .blogger{
        text-align: center;
    }
</style>

Friend.vue

<template>
    <div class="blogger">
        <h1>{{msg}}</h1>
        <h2 @click="showName">兄弟名字:{{name}}</h2>
        <h2>兄弟性别:{{sex}}</h2>
    </div>
</template>

<script>
import {mixin} from '../mixin'

export default {
    name:'Blogger',
    data() {
        return {
            msg:'栋栋乐',
            name:'贺灵志',
            sex:'男'
        }
    },
    mixins:[mixin]
}
</script>

<style>
    .blogger{
        text-align: center;
    }
</style>

mixin.js

export const mixin = {
    methods:{
        skip() {
        window.location.href="https://xiaoyuan.blog.csdn.net/"
    },
    showName() {
        alert(this.name)
    }
  }  
}

 

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

才疏学浅的小缘同学

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值