【vue随手笔记】Vue设置静态常量
2018-08-28 本文已影响59人
stormKid
1、自定义目录:
在src 下建立 constants 目录 如图所示:

2、自定义js:
在新建好的constants中建立自己自定义的JS文件,如图所示:

3、在该自定义js文件中写入代码:
const BASE_URL = "https://www.baidu.com"
export default{
BASE_URL
}
4、在route目录下的index.js中导包并赋值全局变量:
import net from "@/constants/Net.Constants"
Vue.prototype.NET = net
5、使用静态变量:
<template>
<button v-on:click="test">
测试
</button>
</template>
<script>
export default {
methods:{
test: function(){
console.log('====================================');
console.log(this.NET.BASE_URL);
console.log('====================================');
}
}
}
</script>
<style lang="less" scoped>
</style>
5、点击效果:
