Apply global variable to Vuejs

前端 未结 8 1411
谎友^
谎友^ 2020-12-02 15:02

I have a javascript variable which I want to pass globally to Vue components upon instantiation thus either each registered component has it as a property or it can be acces

8条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-12-02 15:41

    You can use mixin and change var in something like this.

    // This is a global mixin, it is applied to every vue instance
    Vue.mixin({
      data: function() {
        return {
          globalVar:'global'
        }
      }
    })
    
    Vue.component('child', {
      template: "
    In Child: {{globalVar}}
    " }); new Vue({ el: '#app', created: function() { this.globalVar = "It's will change global var"; } });
    
    
    In Root: {{globalVar}}

提交回复
热议问题