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

78
Vistas
Vue 3 globalProperties reactivity

I try to migrate Vue 2 plugin to Vue 3, and in install function i have construction like this:

install(Vue, options) {
  const reactiveObj = {
    // ...
  };

  Vue.prototype.$obj = Vue.observable(reactiveObj);
}

And then I can access it with this.$obj in any component and it is reactive when reactiveObj changes. But in Vue 3 I try to make something like this:

import {reactive} from 'vue';

install(app, options) {
  const reactiveObj = reactive({
    // ...
  });

 app.config.globalProperties.$obj = reactiveObj;
}

And then I can access it with this.$obj, it is Proxy object, but it is not reactive when reactiveObj changes. How can I make it reactive?

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

0

I post the codes that works for me when I changing the value of $obj in my component. Maybe it could help you to understand the problems in your code. here is the "myPlugin.js" file where I defined the plugin:

myPlugin.js:

import {reactive} from 'vue';

export default {
    install(app, options) {
        const reactiveObj = reactive({
            id: 1,
            name: "my-name"
        });

        app.config.globalProperties.$obj = () => {
            return reactiveObj
        };
    }
}

And here is the registration of plugin in my main.js file:

import { createApp } from 'vue'

import App from './App.vue'

import myPlugin from "./plugins/myPlugin"

const app = createApp(App)

app.use(myPlugin);
app.mount('#app')

And here is the code of my component where by clicking the button the values of $obj is changed and you can see that it is reactive:

component.vue:

<template>
  <div>
    <p>{{ $obj() }}</p>
    <button @click="myFunc">click to change</button>
  </div>
</template>

<script>
import {getCurrentInstance, reactive} from "vue";
export default {
  setup() {
    const thisApp= getCurrentInstance()
    const reactiveObj1 = reactive({
      id: 2,
      name: "new-name"
    });

    const myFunc = function () {
      thisApp.appContext.config.globalProperties.$obj().name = reactiveObj1.name
      thisApp.appContext.config.globalProperties.$obj().id = reactiveObj1.id
    }

    return {
      reactiveObj1,
      myFunc
    }

  }
}
</script>

I used getCurrentInstance in my component, because I am using composition API style. Maybe you do not need that if you are using "options API" style.

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