vue ---通过API接口获取数据

时间:2020-06-06 23:24:48   收藏:0   阅读:355

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 config
return 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.data
console.log(res)
},

 


```

 

评论(0
© 2014 mamicode.com 版权所有 京ICP备13008772号-2  联系我们:gaon5@hotmail.com
迷上了代码!