Jay*_*n H 9 javascript vue.js vuex
我正在尝试使用在vuex中使用mapGetters函数提取的数据来创建计算属性,但我总是未定义,直到页面/ dom完全加载.
这是一个用于隐藏/显示某些按钮的isRegistered computed属性的示例.
computed: {
...mapGetters(['solos', 'user']),
isRegistered () {
return this.solos.registered.indexOf(this.user._id) !== -1
}
}
Run Code Online (Sandbox Code Playgroud)
以下是使用isRegistered computed属性的按钮的HTML.
<a href="#" class="register-button" v-if="!isRegistered">REGISTER NOW</a>
<a href="#" class="registered-button" v-if="isRegistered">REGISTERED</a>
Run Code Online (Sandbox Code Playgroud)
我正在通过我在创建的函数中调用的动作设置getter
created () {
this.getTournament('solos').catch(err => {})
}
Run Code Online (Sandbox Code Playgroud)
这是我设置相应getter的操作
getTournament: ({commit}, type) => {
return feathers.service('tournaments').find({
query: {
type: type,
status: 'registering'
}
}).then(tourney => {
commit(type.toUpperCase(), tourney.data[0])
})
}
Run Code Online (Sandbox Code Playgroud)
这是相应的突变
const mutations = {
SOLOS (state, result) {
state.solos = result;
}
};
Run Code Online (Sandbox Code Playgroud)
这是相应的吸气剂
const getters = {
solos (state) {
return state.solos
}
}
Run Code Online (Sandbox Code Playgroud)
我遇到的问题是,在PAGE/DOM完全加载之前,值最初显示为undefined,导致.indexOf抛出以下错误:
TypeError: Cannot read property 'indexOf' of undefined
Run Code Online (Sandbox Code Playgroud)
我能够使其工作的唯一方法是在计算属性上使用if语句来检查状态数据是否已加载.
isRegistered () {
if (this.solos._id) return this.solos.registered.indexOf(this.user._id) !== -1
}
Run Code Online (Sandbox Code Playgroud)
这似乎不是一种正确的做事方式.我究竟做错了什么?
归档时间: |
|
查看次数: |
3976 次 |
最近记录: |