Skip to content

全局属性

Vue2 没有 app 实例,通常直接操作 原型链

main.ts
ts
// main.js
import Vue from 'vue'
import App from './App.vue'

// 定义全局变量
Vue.prototype.$baseUrl = 'https://api.xxx.com'
Vue.prototype.$userInfo = { name: '张三' }

new Vue({
  render: h => h(App),
}).$mount('#app')

在组件中使用:

vue
<script>
export default {
  mounted() {
    console.log(this.$baseUrl) // https://api.xxx.com
    console.log(this.$userInfo.name) // 张三
  }
}
</script>