vue —通过API接口获取数据

1. 配置axios

```

import axios from axios`

 

```

2. 配置根路径

```

axios.defaults.baseURL = 【接口网址】axios.interceptors.request.use(config => {// console.log(config)config.headers.Authorization = window.sessionStorage.getItem(token)// 在最后必须 return configreturn config})Vue.prototype.$http = axios

 

```

3. 获取数据

```

async 【函数名】() {const { data: res } = await this.$http.get(menus)//判断状态if (res.meta.status !== 200) return this.$message.error(res.meta.msg)this.menulist = res.dataconsole.log(res)},

 

```

 

相关文章