温馨提示×

怎么在vue组件中使用全局变量

vue
养鱼的猫咪
975
2021-05-13 17:51:01
栏目: 编程语言

在vue组件中使用全局变量的方法:1.新建vue.js项目;2.使用export方法定义全局变量;3.使用Vue.prototype方法挂载全局变量;4.使用this调用全局变量;

怎么在vue组件中使用全局变量

具体步骤如下:

1.首先,在vue-cli中创建一个vue.js项目;

vue create project-name

2.vue.js项目创建好后,在项目中使用export方法定义全局变量;

export default{

access_token:"",

refresh_token:"",

setAToken(access_token){

this.access_token = refresh_token;

},

setAToken(refresh_token){

this.refresh_token = access_token;

}

}

3.全局变量定义好后,使用Vue.prototype方法将全局变量挂载到vue实例中;

Vue.prototype.defined = defined;

4.最后,全局变量挂载好后,使用this方法即可调用全局变量;

this.defined.access_token;

this.defined.refresh_token;

0