我正在使用:以VueJs 2为起点的全局数据,因为我只想读 /写一个变量。
我在现有代码中添加了@click事件以修改变量,但出现“未捕获的ReferenceError:未定义$ myGlobalStuff”。
谁能看到我在做什么错:
HTML:
<div id="app2">
{{$myGlobalStuff.message}}
<my-fancy-component></my-fancy-component>
<button @click="updateGlobal">Update Global</button>
</div>
Run Code Online (Sandbox Code Playgroud)
VueJS:
var shared = {消息:“我的全局消息”}
shared.install = function(){
Object.defineProperty(Vue.prototype, '$myGlobalStuff', {
get () { return shared }
})
}
Vue.use(shared);
Vue.component("my-fancy-component",{
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
}
}
})
Run Code Online (Sandbox Code Playgroud)
如您所见,我向现有代码添加的内容很少,而且效果很好。
Any help on what I am overlooking would be appreciated.