Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

148
Vistas
Is there a way to add Vuex to Vue plugin install function and share state between its components?

Basically i want to use my own store inside vue install and vue components i add throught my vue plugin.

I found that i can use vuex inside actual install function and do everything vuex can, but i can't seem to pass it to my custom components.

Is there any other ways to add global events in my SFC's that i add inside my plugin?

import MyComponent from './MyComponent.vue'
import MySecondComponentfrom './MySecondComponent.vue'
import Vuex from 'vuex'
import store from './store.js'

const CustomFn = function(message, options) {
    this.$store.dispatch("someRandomAction", { message, options })
}

export default {
    install(Vue, options) {
        if (this.installed) { return }
        this.installed = true

        Vue.use(Vuex)

        const storeInstance = new Vuex.Store(store)

        Vue['store'] = storeInstance

        // is there a way to add storeInstance to my own components and share one state between them?
        Vue.component('MyComponent', Vue.extend(MyComponent))
        Vue.component('MySecondComponent', Vue.extend(MySecondComponent))

        Vue.prototype['$doStuff'] = CustomFn
        Vue['doStuff'] = CustomFn
    }
}

Problem is that I cannot pass this Vuex store instance inside MyComponent and share the same store between multiple components.

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

Custom component properties are supposed to be globally provided in Vue 2 by assigning them to Vue.prototype because component instances prototypically inherit from it.

new Vuex.Store(store) is a mistake if store is already Vuex store and not plain object that contains store options. This will result in malfunctioning store like shown in this question. Even if store is not store instance yet, it makes sense to make it an instance and export in a separate module so it could be imported in non-component modules.

It should be:

Vue.prototype.$store = store
about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda