Vue文件配置全局变量

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/weixin_38362455/article/details/81570502

首先新建一个.vue 文件用来定义全局变量 GLOBAL.vue

<!--全局变量配置文件-->
<script type="text/javascript">

     var BASE_URL="http://127.0.0.1:8080";
     /*暴露变量*/
     export default{
       BASE_URL
     }

</script>

然后到main.js 中引用并将Global.vue 设置为 Vue的属性

//引入全局变量
import global from './pages/Global'
Vue.prototype.GLOBAL = global

在任意的.vue界面都可以直接使用
在js 代码中可以 this.GLOBAL.BASE_URL 这样引用变量
在界面中可以用{{this.GLOBAL.BASE_URL}} 引用

猜你喜欢

转载自blog.csdn.net/weixin_38362455/article/details/81570502