What is the best way to create a constant, that can be accessible from entire application in VueJs ?

前端 未结 4 637
难免孤独
难免孤独 2021-01-17 16:43

I can create a constant through a store in my vuejs application, but i don\'t think it is a good practice.what is other way to do the same?

4条回答
  •  渐次进展
    2021-01-17 17:05

    You can always define a variable outside of the Vue app scope and use it throughout the application.

    However, if you are using any bundler like Webpack/Browserify/etc. you can do the same but you'd have to import it into every component using it. An example for that can be found below.

    //const.js
    export default {
       c1: 'Constant 1',
       c2: 'Constant 2'
    }
    
    // component.vue
    import const from './const';
    
    export default {
      methods: {
        method() {
          return const.c1;
        }
      }
    }
    

提交回复
热议问题