I have a code code snippet which works fine. header.vue
onLogout(){
this.logout({ router: this.$router });
}
store.js (actions)
logout({commit}, {router}){
commit('clearAuthData')
router.replace('/')
}
What this does is onLogout function clear the idToken and redirect user to the welcome screen. This works fine. But in the same header.vue componet I can't pass the router and read it inside the store.js. It says router is undefined. Why would this happen and how do I fix this? This is my code. 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 (actions)
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')
}
Why isn't this work and is there more efficient way to pass router to the store.js other than passing with each and every time.