VUE 全局变量的定义与使用
2020-04-08 本文已影响0人
_沉默的疯子
创建一个全局变量模块文件,模块中定义变量,用export default 暴露出
创建 global_variable.js
const HOST = '127.0.0.0:5000'
const token = '00000'
export default {
HOST,
token
}
全局使用
global_variable.js文件引入main.js文件,并使用Vue.prototype挂在至vue
// main.js
import global_variable from './static/global_variable'
Vue.prototype.GLOBAL_VARIABLE = global_variable
// test.vue
getHost() {
this.host = 'HOST: ' + this.GLOBAL_VARIABLE.HOST
}
局部引用
// test.vue
import GLOBAL_VARIABLE from "../static/global_variable"
getHost() {
this.host = 'HOST: ' + GLOBAL_VARIABLE.HOST
}