vue.js – 在所有组件中更改Vue原型变量

我想在整个页面中更改下面的全局变量

Vue.prototype.$color = 'green';

我尝试使用下面的代码,但它只在我创建的组件中更改

watch: {
   cor(newValue,oldVlue) {
       this.$color = newValue;
   }
}

我是否可以创建一种方法来更改页面所有组件的原型变量?

解决方法

要全局使用$color,您可以使用 Mixin,更具体地说是 Global Mixin.

如果您只希望它是只读的,那么这是最简单的解决方案(更少的代码).请参阅代码段:

Vue.mixin({
  created: function () {
    this.$color = 'green';
  }
})

new Vue({
  el: '#app1',data: {
    message: 'Hello Vue.js!'
  },mounted() {
    console.log('$color #app1:',this.$color);
  }
})

new Vue({
  el: '#app2',mounted() {
    console.log('$color #app2:',this.$color);
  }
})
<script src="https://unpkg.com/vue@2.5.15/dist/vue.min.js"></script>

<div id="app1">
  <p>app1: {{ message }}</p>
</div>

<div id="app2">
  <p>app2: {{ message }}</p>
</div>

使颜色反应灵敏

要使Vue在任何地方做出反应以改变$color,您可以使用Vuex商店(see other answer).

但是如果您不想仅仅为此使用Vuex,另一种可能性是创建一个Vue实例来保存“共享”数据.之后,创建一个带有计算属性的mixin,该属性引用此“共享”Vue实例的$data.见下面的演示.

// not using a Vuex store,but a separated Vue instance to hold the data
// only use this if you REALLY don't want to use Vuex,because Vuex is preferrable
let globalData = new Vue({
  data: { $color: 'green' }
});
Vue.mixin({
  computed: {
    $color: {
      get: function () { return globalData.$data.$color },set: function (newColor) { globalData.$data.$color = newColor; }
    }
  }
})

// this.$color will be available in all Vue instances...
new Vue({
  el: '#app1'
})
new Vue({
  el: '#app2'
})
// ...and components
Vue.component('my-comp',{template: '#t3'});
new Vue({
  el: '#app3',})
<script src="https://unpkg.com/vue@2.5.15/dist/vue.min.js"></script>

<div id="app1">Color: {{ $color }} <button @click="$color = 'red'">change to red</button></div>
<div id="app2">Color: {{ $color }} <button @click="$color = 'yellow'">change to yellow</button></div>

<template id="t3">
  <div>Color: {{ $color }} <button @click="$color = 'purple'">change to purple</button></div>
</template>
<div id="app3"><my-comp></my-comp></div>

为了完整性,请查看下面的内容,了解如何使用Vuex和Mixin(有关如何使用Vuex in the other answer的更多详细信息).

// Using a Vuex to hold the "shared" data
// The store is not added to any instance,it is just referenced directly in the mixin
const store = new Vuex.Store({
  state: { $color: 'green' },mutations: { update$color: function(state,newColor) { state.$color = newColor; } }
});
Vue.mixin({
  computed: {
    $color: {
      get: function() { return store.state.$color },set: function(newColor) { return store.commit('update$color',newColor); }
    }
  }
})


// this.$color will be available in all Vue instances...
new Vue({
  el: '#app1'
})
new Vue({
  el: '#app2'
})
// ...and components
Vue.component('my-comp',})
<script src="https://unpkg.com/vue@2.5.15/dist/vue.min.js"></script>
<script src="https://unpkg.com/vuex@3.0.1/dist/vuex.min.js"></script>

<div id="app1">Color: {{ $color }} <button @click="$color = 'red'">change to red</button></div>
<div id="app2">Color: {{ $color }} <button @click="$color = 'yellow'">change to yellow</button></div>

<template id="t3">
  <div>Color: {{ $color }} <button @click="$color = 'purple'">change to purple</button></div>
</template>
<div id="app3"><my-comp></my-comp></div>

相关文章

前言 做过web项目开发的人对layer弹层组件肯定不陌生,作为l...
前言 前端表单校验是过滤无效数据、假数据、有毒数据的第一步...
前言 图片上传是web项目常见的需求,我基于之前的博客的代码...
前言 导出Excel文件这个功能,通常都是在后端实现返回前端一...
前言 众所周知,js是单线程的,从上往下,从左往右依次执行,...
前言 项目开发中,我们可能会碰到这样的需求:select标签,禁...