Javascript Vue未更新全局$prototype变量

Javascript Vue未更新全局$prototype变量,javascript,vue.js,prototype,Javascript,Vue.js,Prototype,我正在尝试在Vue中创建一个全局变量,可以在整个应用程序中读取和更新该变量。我决定使用prototype方法,但由于某种原因,变量得到了更新(console.log发生了变化),但前端()没有得到更新。请有人向我解释一下为什么会这样做,以及我如何解决这个“全局变量”问题 main.js: Vue.prototype.$something = 'Hi there!'; // I tried this but it gives the same result // Vue.prototype.$s

我正在尝试在Vue中创建一个全局变量,可以在整个应用程序中读取和更新该变量。我决定使用prototype方法,但由于某种原因,变量得到了更新(console.log发生了变化),但前端()没有得到更新。请有人向我解释一下为什么会这样做,以及我如何解决这个“全局变量”问题

main.js

Vue.prototype.$something = 'Hi there!';

// I tried this but it gives the same result
// Vue.prototype.$something = { text: 'Hi there!' };
组件1.vue

<label>{{$something}}</label>
<label>{{$something}}</label>
<button v-on:click="changeThat()">Change</button>
...
changeThat() {
  console.log(this.$something); // 'Hi there!'
  this.$something = "Good bye!";
  console.log(this.$something); // 'Good bye!'
}
我希望这两个
发生变化时更新。$something
发生变化。

您可以这样使用:

Vue.prototype.$globalData = Vue.observable({
  label: 'test' 
});
{{ $globalData.label }}
在应用程序的任何组件中,您都可以这样使用它:

Vue.prototype.$globalData = Vue.observable({
  label: 'test' 
});
{{ $globalData.label }}
如果在任何地方更改
标签
,如下所示:

this.$globalData.label = 'new test';
它将反映在所有组件中。

您可以这样使用:

Vue.prototype.$globalData = Vue.observable({
  label: 'test' 
});
{{ $globalData.label }}
在应用程序的任何组件中,您都可以这样使用它:

Vue.prototype.$globalData = Vue.observable({
  label: 'test' 
});
{{ $globalData.label }}
如果在任何地方更改
标签
,如下所示:

this.$globalData.label = 'new test';

这会反映在所有组件中。

我不认为vue管理原型我不认为vue管理原型100%完美答案。谢谢!:)100%完美答案。谢谢!:)