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

眉间皱痕 提交于 2020-01-03 09:29:12

问题


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?


回答1:


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;
    }
  }
}



回答2:


try this instead

//conts.js
const test = "texte";

export default test
//component.vue
import test from "./conts";

<template>
  <div>
   {{example}}
  </div>
</template>

export default {
  data: function(){
   return {
    example: test
  }
 }
}


来源:https://stackoverflow.com/questions/46882944/what-is-the-best-way-to-create-a-constant-that-can-be-accessible-from-entire-ap

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!