Luk*_*kas 3 javascript vue.js vuex vuejs2
我想访问in的state属性currentUser以vuex在其中user.js使用它,auth.js但如果我写它不起作用:store.state.currentUser.uid ===...
我得到这个错误:
无法读取未定义的属性“状态”。
代码中缺少什么?
那个树:
src
store
user
|>user.js
|>mutations.js
index.js
auth.js
Run Code Online (Sandbox Code Playgroud)
用户.js:
const state = {
profile: {},
loggedIn: false,
currentUser: null,
userProfile: {}
}
export default {
namespaced: true,
state
}
Run Code Online (Sandbox Code Playgroud)
auth.js:
import store from './store'
const fb = require('./firebaseConfig.js')
fb.auth.onAuthStateChanged(user => {
if (user) {
fb.postsCollection.orderBy('createdOn', 'desc').onSnapshot(querySnapshot => {
let createdByCurrentUser
if (querySnapshot.docs.length) {
createdByCurrentUser = store.state.currentUser.uid ===
querySnapshot.docChanges()[0].doc.data().userId ? 'Yes' : 'No'
console.log(createdByCurrentUser)
}
})
}
})
Run Code Online (Sandbox Code Playgroud)
索引.js
import Vue from 'vue'
import Vuex from 'vuex'
import user from '@/store/user'
Vue.use(Vuex)
const store = new Vuex.Store({
state: {
awesome: true
},
modules: {
user
}
})
export default store
Run Code Online (Sandbox Code Playgroud)
您需要使用getters,查看文档
getters.js
export default {
currentUser
}
function currentUser({ currentUser }) {
return currentUser;
}
Run Code Online (Sandbox Code Playgroud)
用户.js
import getters from './getters'
export default {
namespaced: true,
state: {
profile: {},
loggedIn: false,
currentUser: null,
userProfile: {}
},
getters
}
Run Code Online (Sandbox Code Playgroud)
认证.js
import store from './store';
const currentUser = store.getters['user/currentUser'];
Run Code Online (Sandbox Code Playgroud)
因为您命名了模块,所以您必须使用它的名称作为前缀来访问 getter 函数
| 归档时间: |
|
| 查看次数: |
3538 次 |
| 最近记录: |