P粉2421267862023-07-29 17:58:49
In Vue 3, you can still use mixins in a similar way to Vue 2 when using the Options API.
const mixin = { created() { console.log('Mixin'); }, }; export default { name: "Modal", components: { Loader }, mixins: [mixin] };
But for the Composition API, you must use composable functions instead of mixins:
// Composable declaration function import { onMounted } from 'vue'; export function useMixin() { onMounted(() => console.log('Mixin')); return {}; } // In your component import { useMixin } from './mixin'; import Loader from './Loader'; export default { name: "Modal", components: { Loader }, setup() { useMixin(); return {}; }, };
Combinable functions provide a clearer and more flexible alternative to mixins when using Vue 3's Composition API.