fetchTaskList const token = localStoragegetItemtoken; axiosinterceptorsrequestuse config = configheadersAuthorization = token; return config;
这段代码没有明显的错误,但是需要注意的是:
- 需要在请求头中添加Authorization字段,才能携带token进行请求。
- 在请求返回后,需要从response.data中获取数据,而不是直接使用response。因为axios返回的response包含很多信息,而我们只需要其中的数据部分。
- 在catch中进行错误处理,避免程序崩溃。
修改后的代码如下:
fetchTaskList() {
const token = localStorage.getItem('token');
axios.interceptors.request.use(
config => {
config.headers.Authorization = token;
return config;
},
error => Promise.reject(error)
);
axios.get('http://localhost:8888/task/findAllTask')
.then(response => {
this.taskList = response.data.data.records;
this.nextId = response.data.data.total;
console.log(this.taskList);
})
.catch(error => {
console.log(error);
});
}
原文地址: https://www.cveoy.top/t/topic/bamI 著作权归作者所有。请勿转载和采集!