Apply global variable to Vuejs

前端 未结 8 1407
谎友^
谎友^ 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:34

    You can use a Global Mixin to affect every Vue instance. You can add data to this mixin, making a value/values available to all vue components.

    To make that value Read Only, you can use the method described in this stackoveflow answer.

    Here is an example:

    // This is a global mixin, it is applied to every vue instance. 
    // Mixins must be instantiated *before* your call to new Vue(...)
    Vue.mixin({
      data: function() {
        return {
          get globalReadOnlyProperty() {
            return "Can't change me!";
          }
        }
      }
    })
    
    Vue.component('child', {
      template: "<div>In Child: {{globalReadOnlyProperty}}</div>"
    });
    
    new Vue({
      el: '#app',
      created: function() {
        this.globalReadOnlyProperty = "This won't change it";
      }
    });
    <script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.1.3/vue.js"></script>
    <div id="app">
      In Root: {{globalReadOnlyProperty}}
      <child></child>
    </div>

    0 讨论(0)
  • 2020-12-02 15:37

    In your main.js file, you have to import Vue like this :

    import Vue from 'vue'
    

    Then you have to declare your global variable in the main.js file like this :

    Vue.prototype.$actionButton = 'Not Approved'
    

    If you want to change the value of the global variable from another component, you can do it like this :

    Vue.prototype.$actionButton = 'approved'
    

    https://vuejs.org/v2/cookbook/adding-instance-properties.html#Base-Example

    0 讨论(0)
  • 2020-12-02 15:39

    Just Adding Instance Properties

    For example, all components can access a global appName, you just write one line code:

    Vue.prototype.$appName = 'My App'

    $ isn't magic, it's a convention Vue uses for properties that are available to all instances.

    Alternatively, you can write a plugin that includes all global methods or properties.

    0 讨论(0)
  • 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: "<div>In Child: {{globalVar}}</div>"
    });
    
    new Vue({
      el: '#app',
      created: function() {
        this.globalVar = "It's will change global var";
      }
    });
    <script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.1.3/vue.js"></script>
    <div id="app">
      In Root: {{globalVar}}
      <child></child>
    </div>

    0 讨论(0)
  • 2020-12-02 15:45

    In VueJS 3 with createApp() you can use app.config.globalProperties

    Like this:

    const app = createApp(App);
    
    app.config.globalProperties.foo = 'bar';
    
    app.use(store).use(router).mount('#app');
    

    and call your variable like this:

    app.component('child-component', {
      mounted() {
        console.log(this.foo) // 'bar'
      }
    })
    

    doc: https://v3.vuejs.org/api/application-config.html#warnhandler

    If your data is reactive, you may want to use VueX.

    0 讨论(0)
  • 2020-12-02 15:47

    you can use Vuex to handle all your global data

    0 讨论(0)
提交回复
热议问题