首页 |  我的博客 |  查看该博主内容分类 | 

node框架Vue如何使用mixins将组件直接拆分出来,如将methods整块拆出来?(嵌入/插入一个方法)

  1. 创建一个混入文件(例如 mixin.js)
// mixin.js
export const myMixin = {
  methods: {
    sharedMethod() {
      console.log('This is a shared method from mixin');
    },
    anotherSharedMethod() {
      console.log('This is another shared method from mixin');
    }
  }
}
  1. 导入混入
<template>
  <div>
	<!--        可以直接使用-->
    <button @click="sharedMethod">Use sharedMethod from Mixin</button>
    <button @click="anotherSharedMethod">Use anotherSharedMethod from Mixin</button>
  </div>
</template>

<script>
import { myMixin } from './myMixin.js';

export default {
  name: 'MyComponent',
  mixins: [myMixin],
  data(){
  	return {}
  },
  ...
}
</script>

posted @ 2024-06-13 16:17  Z哎呀  阅读(31)  评论(0)    收藏  举报