Apply global variable to Vuejs

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

提交回复
热议问题