search

Home  >  Q&A  >  body text

Vue Mixins imported globally but only used in local components

I want to use a mixin in multiple components without having to write import and use declarations each time. I've tried connecting the mixin object to the global variable via vue.prototype, but the mixins are added to the component before the global variable is accessible. Also, I can't import the mixin globally without adding it to all components globally (which I don't want to do). If anyone has a suitable solution that doesn't involve too much code I'd be grateful. Since this is my first question here, please feel free to leave suggestions for improvements.

Edit: I can import the mixin globally, but I want to define myself which components use the mixin.

Edit 2: Another solution is in array of mixins, but I didn't find a way to do this. Neither require() nor import() can be used.

Edit 3: I decided to stick with using the mixin locally.

P粉436052364P粉436052364272 days ago341

reply all(1)I'll reply

  • P粉754473468

    P粉7544734682024-02-27 16:21:03

    Have you tried this?

    main.js

    import yourGlobalMixin from '@/mixins/yourGlobalMixin.js'
    
    
    Vue.mixin(yourGlobalMixin)
    
    

    reply
    0
  • Cancelreply