在我的 Laravel 5.8 / "vue": "^2.6.10"/ "vuex": "^3.1.0" app 在 resources/js/components/Login.vue 文件中我有方法
methods: {
authenticate() {
this.$store.dispatch('login'); // calling action
login(this.$data.form)
.then((res) => {
this.$store.commit("setLoginSuccess", res); // calling mutation
this.$store.dispatch('retrieveHostelBookmarks', res.user.id);
this.$store.dispatch('retrievePersonalOptions', res.user.id);
this.$router.push({path: '/personal'}); // For debugging!
})
.catch((error) => {
console.log("=== error::")
console.log(error)
this.$store.commit("setLoginFailed", {error}); // calling mutation
});
}
Run Code Online (Sandbox Code Playgroud)
在 resources/js/helpers/authFuncs.js 我有定义:
export function login(credentials) {
return new Promise((res, rej) => {
axios.post('/api/auth/login', credentials)
.then((response) => {
setAuthorizationToken(response.data.access_token);
res(response.data);
})
.catch((err) =>{
console.error(err)
rej("Wrong email or password");
})
})
}
Run Code Online (Sandbox Code Playgroud)
问题是,在控制台中的无效凭据上,我在输出末尾看到了 promise 警告:
VM836:1 POST http://127.0.0.1:8084/api/auth/login 401 (Unauthorized)
(anonymous) @ VM836:1
dispatchXhrRequest @ app.js?dt=1571914585:311
xhrAdapter @ app.js?dt=1571914585:150
dispatchRequest @ app.js?dt=1571914585:758
Promise.then (async)
request @ app.js?dt=1571914585:560
Axios.<computed> @ app.js?dt=1571914585:585
...
app.js?dt=1571914585:10042 === error::
app.js?dt=1571914585:10043 Wrong email or password
app.js?dt=1571914585:131483 Uncaught (in promise) NavigationDuplicated {_name: "NavigationDuplicated", name: "NavigationDuplicated", message: "Navigating to current location ("/login") is not allowed", stack: "Error? at new NavigationDuplicated (http://127.…/127.0.0.1:8084/js/app.js?dt=1571914585:148080:12"}
Run Code Online (Sandbox Code Playgroud)
为什么会出现此警告以及如何解决?
看起来决定很简单,因为 error.response 引用了当前页面:
if ( error.response.config.url != '/api/auth/login' ) {
router.push('/login');
}
Run Code Online (Sandbox Code Playgroud)
这对我有用......
| 归档时间: |
|
| 查看次数: |
8748 次 |
| 最近记录: |