gpt4 book ai didi

typescript - Vuex Module Decorators : Access vuex module from this. $store 没有注册模块

转载 作者:行者123 更新时间:2023-12-04 10:40:08 28 4
gpt4 key购买 nike

在我的项目中,我使用 vuex-module-decorators 创建了一个小型 vuex 模块。 .但是当试图直接从 this.$store 访问它时或 $store在模板部分,它不会触发注册。

这是一个例子:

// exampleModule.ts
import { Module, getModule, Mutation, VuexModule } from 'vuex-module-decorators';
import store from '@/store';

@Module({
dynamic: true,
namespaced: true,
name: 'example',
stateFactory: true,
store,
})
class Example extends VuexModule {
private work: boolean = false;

get isWorking() {
return this.work;
}

@Mutation
setWorking(status: boolean) {
this.work = status;
}
}

export default getModule(Example);

// App.vue
<template>

<div>
first attempt: {{ $store.getters['example/isWorking'] }} // this is not working
second attempt: {{ isWorking }} // this is not working too
</div>
</template>

<script lang="ts">
import Vue from 'vue';

export default Vue.extend({
computed: {
isWorking() {
return this.$store.getters['example/isWorking'];
}
}
});
</script>

我真正注册模块并使其工作的唯一方法是将其直接导入组件中,如下所示:

<script lang="ts">
import Vue from 'vue';
import exampleModule from '@/store/modules/example/exampleModule';

export default Vue.extend({
computed: {
isWorking() {
return exampleModule.isWorking;
}
}
});
</script>

我在这里错过了什么吗?这是想要的行为吗?谢谢。

顺便说一句,我在他们的 github 存储库上打开了一个问题,但我仍然没有任何答案,所以我在这里

https://github.com/championswimmer/vuex-module-decorators/issues/190

最佳答案

我无法评论,所以我会在这里回答。
您是否在 main.js 文件中导入了您的商店?
也许这就是问题所在。
祝你好运!

关于typescript - Vuex Module Decorators : Access vuex module from this. $store 没有注册模块,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59967515/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com