mixin(混入)

  • 功能:可以把多个组件共用的配置提取成一个混入对象,不需要在每个组件中都写了

【一】定义mixin

  • mxin//index.js
const  hunru={
    data(){
        return {
            title:"首页"
        }
    },
    methods:{
        handleChange(){
            this.title=this.title+'_NB'
        }
    }
}
export  default hunru

【二】在组件中局部使用

  • data 中方的是当前的那么页面中默认的就是当前的
<template>
  <div class="home">
    <h1>{{ title }}</h1>
    <button @click="handleChange">点我变标题</button>
  </div>
</template>

<script>

import hunru from '@/mixin'

export default {
  name: 'AboutView',

  data () {
    return {}
  },
  methods: {},
  mixins: [hunru] # 如果用多个,数组继续往后拼
}
</script>

image-20240430162747019

【三】在全局中使用

Vue.mixin(h)
posted @ 2024-05-08 18:38  -半城烟雨  阅读(16)  评论(0)    收藏  举报