在 vue js laravel 中刷新后默认 axios 访问令牌不起作用

Default axios access token not working after refresh in vue js laravel

我们在 axios.js 中为 axion 请求声明了默认授权 header。它第一次工作但是当我刷新页面时它给出 500 internal server 错误。

这是我在 axios.js

中声明的方式
// axios
import axios from 'axios'

alert(localStorage.getItem('accessToken'))
const baseURL = ''
axios.defaults.headers.common = { 'Authorization': localStorage.getItem('accessToken') }
export default axios.create({
    baseURL
    // You can add your headers here
})

Laravel api 路线

Route::group(['middleware' => 'auth:api'], function () {
    Route::group(['prefix' => 'user'], function () {
        Route::get('/all_users', 'User\UserController@index');
    });
});

这是回应

Symfony\Component\Routing\Exception\RouteNotFoundException: Route [login] not defined. in file C:\projects\logistics-portal\vendor\laravel\framework\src\Illuminate\Routing\UrlGenerator.php on line 420

你的失踪Bearer

所以添加这个像

axios.defaults.headers.common = { 'Authorization': 'Bearer '+localStorage.getItem('accessToken') }