我試圖創建一個listenAuth功能手錶在火力「onAuthStateChanged」通知vuex店當用戶有登錄或退出。據我所知,我只使用突變處理程序修改state.authData,除非我失去了一些東西?Vuex&VueJS(不要變異突變處理之外vuex商店狀態)
,我發現了錯誤:
[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: {}
}
您可能需要升級到Vuex 2.0 –