我正在尝试使用 vuejs 和 laravel 的护照对用户进行身份验证。
我无法弄清楚如何通过操作将多个参数发送到 vuex 突变。
- 店铺 -
export default new Vuex.Store({
state: {
isAuth: !!localStorage.getItem('token')
},
getters: {
isLoggedIn(state) {
return state.isAuth
}
},
mutations: {
authenticate(token, expiration) {
localStorage.setItem('token', token)
localStorage.setItem('expiration', expiration)
}
},
actions: {
authenticate: ({
commit
}, token, expiration) => commit('authenticate', token, expiration)
}
})
- 登录方式 -
login() {
var data = {
client_id: 2,
client_secret: '**************************',
grant_type: 'password',
username: this.email,
password: this.password
}
// send data
this.$http.post('oauth/token', data)
.then(response => {
// send the parameters to the action
this.$store.dispatch({
type: 'authenticate',
token: response.body.access_token,
expiration: response.body.expires_in + Date.now()
})
})
}
我会非常感谢任何形式的帮助!
原文由 Schwesi 发布,翻译遵循 CC BY-SA 4.0 许可协议
突变需要两个参数:
state
和payload
,其中存储的当前状态由 Vuex 本身作为第一个参数传递,第二个参数包含您需要传递的任何参数。传递多个参数 的最简单方法是 破坏它们:
然后稍后在您的操作中,您可以简单地