我正在尝试创建一个listenerAuth函数,该函数在 firebase 中监视“ onAuthStateChanged ”,以在用户登录或退出时通知vuex商店。据我所知,我只是使用突变处理程序修改 state.authData ,除非我遗漏了什么?
我收到错误:
[vuex] Do not mutate vuex store state outside mutation handlers.
这是我的 App.vue javascript(来自我的组件)
<script>
// import Navigation from './components/Navigation'
import * as actions from './vuex/actions'
import store from './vuex/store'
import firebase from 'firebase/app'
export default {
store,
ready: function () {
this.listenAuth()
},
vuex: {
actions,
getters: {
authData: state => state.authData,
user: state => state.user
}
},
components: {
// Navigation
},
watch: {
authData (val) {
if (!val) {
this.redirectLogin
this.$route.router.go('/login')
}
}
},
methods: {
listenAuth: function () {
firebase.auth().onAuthStateChanged((authData) => {
this.changeAuth(authData)
})
}
}
}
</script>
这是我的操作(changeAuth)功能
export const changeAuth = ({ dispatch, state }, authData) => {
dispatch(types.AUTH_CHANGED, authData)
}
这是我的商店(重要的部分)
const mutations = {
AUTH_CHANGED (state, authData) {
state.authData = authData
}
}
const state = {
authData: {}
}