Vue中混入的作用是分发组建中可复用的功能
新建mixins文件夹,新建mixins.ts文件
import { ref } from 'vue';
export default function () {
const num = ref(0);
const fav = ref(false);
const fvbtn = () => {
num.value += 1;
fav.value = true;
setTimeout(() => {
fav.value = false;
}, 3000);
};
return {
num,
fav,
fvbtn,
};
}
在文件中引用
<!--
* @Author: wangyf 1758985226@qq.com
* @Date: 2023-07-04 17:32:24
* @LastEditors: wangyf 1758985226@qq.com
* @LastEditTime: 2023-07-05 08:57:17
* @FilePath: \fast-vue3\src\pages\mixinsVue\index.vue
* @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
-->
<template>
<div>
<h1>混入</h1>
{{ num }}<br />
<button @click="fvbtn">
{{ fav ? '收藏中...' : '收藏' }}
</button>
<hr />
<A />
</div>
</template>
<script setup>
import A from './A.vue';
import mixin from '../mixins/mixins.ts';
const { num, fav, fvbtn } = mixin();
</script>
<style></style>
注意:二者的函数复用,但是数据不共享