admin管理员组文章数量:1356234
I am using : Global data with VueJs 2 as my starting point as I only want to R/W one variable.
I have added an @click event to the existing code to modify the variable, but I get an "Uncaught ReferenceError: $myGlobalStuff is not defined".
Can anyone see what I am doing wrong:
HTML:
<div id="app2">
{{$myGlobalStuff.message}}
<my-fancy-ponent></my-fancy-ponent>
<button @click="updateGlobal">Update Global</button>
</div>
VueJS:
var shared = { message: "my global message" }
shared.install = function(){
Object.defineProperty(Vue.prototype, '$myGlobalStuff', {
get () { return shared }
})
}
Vue.use(shared);
Vueponent("my-fancy-ponent",{
template: "<div>My Fancy Stuff: {{$myGlobalStuff.message}}</div>"
})
new Vue({
el: "#app2",
mounted(){
console.log(this.$store)
},
methods: {
updateGlobal: function() {
$myGlobalStuff.message = "Done it!"
return
}
}
})
As you can see I am adding very little to the existing code, and that works well.
Any help on what I am overlooking would be appreciated.
I am using : Global data with VueJs 2 as my starting point as I only want to R/W one variable.
I have added an @click event to the existing code to modify the variable, but I get an "Uncaught ReferenceError: $myGlobalStuff is not defined".
Can anyone see what I am doing wrong:
HTML:
<div id="app2">
{{$myGlobalStuff.message}}
<my-fancy-ponent></my-fancy-ponent>
<button @click="updateGlobal">Update Global</button>
</div>
VueJS:
var shared = { message: "my global message" }
shared.install = function(){
Object.defineProperty(Vue.prototype, '$myGlobalStuff', {
get () { return shared }
})
}
Vue.use(shared);
Vue.ponent("my-fancy-ponent",{
template: "<div>My Fancy Stuff: {{$myGlobalStuff.message}}</div>"
})
new Vue({
el: "#app2",
mounted(){
console.log(this.$store)
},
methods: {
updateGlobal: function() {
$myGlobalStuff.message = "Done it!"
return
}
}
})
As you can see I am adding very little to the existing code, and that works well.
Any help on what I am overlooking would be appreciated.
Share Improve this question asked Jul 14, 2017 at 20:33 Tony ShermanTony Sherman 2951 gold badge6 silver badges19 bronze badges1 Answer
Reset to default 6Well first, the error you are getting is because you do not reference $myGlobalStuff
using this
. Change to this
this.$myGlobalStuff.message = "Done it!"
And you won't get the error anymore.
But I suspect it won't work the way you are expecting it to, in that, it won't be reactive. I think what you want is for the message to be updated on the page, and that is not really the intent of this code. The original point was just to supply some global values to each Vue or ponent.
To make it reactive we can add one change.
var shared = new Vue({data:{ message: "my global message" }})
Once you do that, message
will be a reactive value.
console.clear()
var shared = new Vue({data:{ message: "my global message" }})
shared.install = function(){
Object.defineProperty(Vue.prototype, '$myGlobalStuff', {
get () { return shared }
})
}
Vue.use(shared);
Vue.ponent("my-fancy-ponent",{
template: "<div>My Fancy Stuff: {{$myGlobalStuff.message}}</div>"
})
new Vue({
el: "#app2",
mounted(){
console.log(this.$store)
},
methods: {
updateGlobal: function() {
this.$myGlobalStuff.message = "Done it!"
return
}
}
})
<script src="https://unpkg./[email protected]/dist/vue.js"></script>
<div id="app2">
{{$myGlobalStuff.message}}
<my-fancy-ponent></my-fancy-ponent>
<button @click="updateGlobal">Update Global</button>
</div>
This is a very naive implementation of how Vuex works. The further you progress down this path, the more features of Vuex you end up implementing.
本文标签: javascriptWriting to a global variable in VueJSStack Overflow
版权声明:本文标题:javascript - Writing to a global variable in VueJS - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744056732a2583391.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论