Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

75
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!