jan*_*doe 2 javascript vue.js vuex
所以我的一个组件中有以下代码:
export default {
name: 'section-details',
components: {
Loading
},
mounted() {
if (!this.lists.length || !this.section_types.length) {
this.$store.dispatch('section/fetch_section_form_data', () => {
if (this.section) {
this.populate_form();
}
});
}
else if (this.section) {
this.populate_form();
}
},
computed: {
section_types() {
return this.$store.state.section.section_types;
},
lists() {
return this.$store.state.list.lists;
},
loading() {
console.log(this.$store.state.section.loading);
this.$store.state.section.loading;
}
},
.
.
.
}
Run Code Online (Sandbox Code Playgroud)
正如您所看到的,我有一个用于“加载”的计算属性,它在执行 ajax 请求时从我的 vuex 存储中检索属性。
在我的 vuex 模块部分,我有这个:
fetch_section_form_data({ commit }, callback) {
commit("isLoading", true);
sectionService
.fetch_form_data()
.then((data) => {
commit("isLoading", false);
commit("fetch_section_types_success", data.section_types);
commit("list/fetch_lists_success", data.lists, { root: true});
if (callback) {
callback();
}
})
.catch((err) => {
commit("isLoading", false);
})
;
}
Run Code Online (Sandbox Code Playgroud)
然后在我对模块的更改中,我有以下代码:
mutations: {
isLoading(state, status) {
state.loading = status;
},
}
Run Code Online (Sandbox Code Playgroud)
最后在我存储加载属性的组件中,我有这个:
<Loading v-if="loading"></Loading>
Run Code Online (Sandbox Code Playgroud)
无论如何,由于某种原因,加载组件没有出现。然而,loading() 方法中的 console.log 对于 this.$store.state.section.loading 返回 true。因此,出于某种原因,Vue 在实际 DOM 中没有发现 loading == true 。任何帮助,将不胜感激。
您需要return从计算属性方法中获取值:
loading() {
return this.$store.state.section.loading;
}
Run Code Online (Sandbox Code Playgroud)