javascript - How to expose a variable in app.vue globally in vue.js - Stack Overflow

I am using vuejs to develop a module of my application, i will be getting data which i need to set to a

I am using vuejs to develop a module of my application, i will be getting data which i need to set to a variable ex this.gblData in the App.vue.

I will build the vue application and integrating it into a another application. Is there a way to expose the variable gblData in the parent application.

I am using vuejs to develop a module of my application, i will be getting data which i need to set to a variable ex this.gblData in the App.vue.

I will build the vue application and integrating it into a another application. Is there a way to expose the variable gblData in the parent application.

Share Improve this question asked Jul 24, 2019 at 16:06 SM079SM079 7833 gold badges11 silver badges32 bronze badges 1
  • How do you instantiate your App? Just store the instance in a global variable and access its .gblData property there. – Bergi Commented Jul 24, 2019 at 16:59
Add a ment  | 

3 Answers 3

Reset to default 3

Option 1: Create a global variable

const gblData = "something";

expose your global variable:

new Vue({
    data:{
        gblData
    }
})

Option2: Define global variable in the prototype.

Vue.prototype.$gblData = "something";

you can now use this variable in any vue ponent like this:

console.log(this.$gblData);

Yes you can pass this variable thhrough window object:

// set
window.gblData = "something";
// get
console.log(window.gblData);

in vue3:

// Vue3

const app = Vue.createApp({})
app.config.globalProperties.gblData = 'something'


app.ponent('a-child-ponent', {
  mounted() {
    console.log(this.gblData) // 'something'
  }
})

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745655336a4638512.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信