admin管理员组文章数量:1026900
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
3 Answers
Reset to default 3Option 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'
}
})
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
3 Answers
Reset to default 3Option 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'
}
})
本文标签: javascriptHow to expose a variable in appvue globally in vuejsStack Overflow
版权声明:本文标题:javascript - How to expose a variable in app.vue globally in vue.js - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745655335a2161566.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论