在项目中遇到一些重复出现的常量,想着写到一个固定的地方可以随取随用;
第一步:在src下面新建一个文件夹,命名const,然后新建一个const.js
.
├── src
│ ├── const
│ │ ├── const.js
│ │
│ └── main.js
└── ...
第二步:在const.js中写入需要的常量
export default {install(Vue,options){Vue.prototype.global = {kindObj:{"dayMap":{title: '日',name: 'day',},"monthMap":{title: '月',name: 'month',},"quarterMap":{title: '季度',name: 'quarter',},"yearMap":{title: '年',name: 'year',},}};}}
第三步:然后在main.js中引入const.js
//引入全局常量
import constant from './const/const.js'
Vue.use(constant);
第四步:在 .vue 组件中使用
//通过js方式使用:
this.global.kindObj
//或在 html 结构中使用
{{global.kindObj}}