【发布时间】:2026-01-14 20:55:02
【问题描述】:
我有一个运行良好的代码代码 sn-p。 header.vue
onLogout(){
this.logout({ router: this.$router });
}
store.js(操作)
logout({commit}, {router}){
commit('clearAuthData')
router.replace('/')
}
它的作用是 onLogout 函数清除 idToken 并将用户重定向到欢迎屏幕。这工作正常。 但是在同一个 header.vue 组件中,我无法通过路由器并在 store.js 中读取它。它说路由器未定义。为什么会发生这种情况,我该如何解决? 这是我的代码。 header.vue
onLogin () {
const formData = {
email: this.email,
password: this.password,
returnSecureToken:true
}
this.$store.dispatch('login',{
email: formData.email,
password: formData.password
},{ router: this.$router })
console.log(formData)
}
store.js(操作)
login({commit},{router},authData){
axios.post('http://localhost/laravel_back/public/api/login',authData
)
.then(res => {
console.log("Backend response",res)
commit('authUser',{
token:res.data[0].token,
userName:res.data[1][0].fname,
id:res.data[2][0].id,
type:res.data.type
})})
.catch(error => console.log(error))
router.replace('/student')
}
为什么这不起作用,除了每次都传递之外,还有更有效的方法将路由器传递给 store.js。
【问题讨论】:
标签: vue.js vuejs2 vue-router