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 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)
},

 


```

 

posted on 2020-06-06 23:01  Qcccccc  阅读(2479)  评论(0)    收藏  举报