简体   繁体   中英

Vue Mixins import global but use only in local components

I want to use a mixin in multiple components without having to write an import and use declaration every time. I already tried connecting the mixin object to a global variable via vue.prototype but mixins get added to components before globals are even accessible. Also i cannot import the mixin globally without also adding it to all components globally (which i don't want). If anyone has a suitable solution that does not include much code i would appreciate it. As this is my 1st question on here feel free to leave suggestions for improvement.

Edit: I'm fine with globally importing the mixin but I want to define myself which components use the mixin.

Edit 2: Another Solution would be an inline import inside the mixins array but i didn't find a way to do it. Neither with require() or import().

Have you tried this?

main.js

import yourGlobalMixin from '@/mixins/yourGlobalMixin.js'


Vue.mixin(yourGlobalMixin)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM