欢迎您访问 最编程 本站为您分享编程语言代码,编程技术文章!
您现在的位置是: 首页

全面解析VUE中的全局变量设定与运用

最编程 2024-01-12 12:09:51
...

创建一个全局变量模块文件,模块中定义变量,用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
}

推荐阅读