简体   繁体   English

将 Vue 3 中的 Vuex 4 模块与 TypeScript 一起使用,以及如何修复循环依赖检查错误?

[英]Using Vuex 4 modules in Vue 3 with TypeScript, and how to fix cyclical dependency linting error?

As of this writing, Vue 3.0 has reached it's first stable v3.0.0 'One Piece' release , with Vuex 4 being in v4.0.0-beta.4 .在撰写本文时,Vue 3.0 已经发布了第一个稳定的v3.0.0 'One Piece' 版本,Vuex 4 处于v4.0.0-beta.4 中

Unfortunately there is no official examples yet on how to use Vuex 4 modules in TypeScript ...不幸的是,目前还没有关于如何在 TypeScript 中使用 Vuex 4 模块的官方示例......

As a further requirement, I want to have store modules state, mutations, getters, and actions in their own files.作为进一步的要求,我希望在它们自己的文件中存储模块状态、突变、getter 和操作。 This makes it easier to manage the code when these modules grow.这使得在这些模块增长时更容易管理代码。

I managed to piece together a working example repository in Github and Codesandbox .我设法GithubCodesandbox 中拼凑了一个工作示例存储库。

By using the examples provided in these resources:通过使用这些资源中提供的示例:

However, a few quirks remains to be solved:但是,仍有一些问题有待解决:

1. Resolving dependency cycle linting errors 1. 解决依赖循环 linting 错误

Currently the typings in a module's index.ts , actions.ts , and getters.ts are dependent on importing RootState from the main store.目前,模块的index.tsactions.tsgetters.ts中的类型依赖于从主存储导入RootState

When using ESLint Airbnb linting config , I'm barraged with linting errors like (currently disabled by // eslint-disable-next-line rules in the repo and example):当使用ESLint Airbnb linting config 时,我遇到了 linting错误,例如(当前被 repo 和示例中的// eslint-disable-next-line规则// eslint-disable-next-line ):

Dependency cycle via @/store/modules/profile > eslint(import/no-cycle)

While this doesn't seem to affect the actual functioning of the store (or does it?), I'd like to know how to overcome this without needing to turn the rule off, or needing to use // eslint-disable-next-line import/no-cycle on those lines?虽然这似乎不会影响商店的实际运作(或者会影响吗?),但我想知道如何在不需要关闭规则或需要使用// eslint-disable-next-line import/no-cycle克服这个问题// eslint-disable-next-line import/no-cycle在那些线上?

I haven't tried if this happens with Standard and Prettier linter configs, or is it related to Airbnb rules?我还没有尝试过这种情况是否发生在 Standard 和 Prettier linter 配置中,还是与 Airbnb 规则有关?

2. How to have a module without any actions? 2. 如何拥有一个没有任何动作的模块?

I tried to have profile module configured without using any actions , however I was unable to get the types rights.我尝试在不使用任何操作的情况下配置配置文件模块,但是我无法获得类型权限。

Currently the example code includes one NON_ACTION action type...当前示例代码包括一种 NON_ACTION 操作类型...

3. How to correctly use namespaced: true module option, and how it affects the usage syntax in components? 3.如何正确使用namespaced: true模块选项,以及它如何影响组件中的使用语法?

Currently profile store is configured with namespaced: true .当前配置文件存储配置有namespaced: true In the App.vue I'm demonstrating it by using mapGetters, which takes a module name as the first parameter.在 App.vue 中,我使用 mapGetters 来演示它,它以模块名称作为第一个参数。 This works.这有效。

However, documents module is without this option, because dispatch with the action type doesn't work anymore.然而,文档模块没有这个选项,因为带有动作类型的调度不再起作用。 It seems to be needing to be used with some other syntax variant which I was unable to find.它似乎需要与我无法找到的其他一些语法变体一起使用。

PS.附注。 Any further code polishing suggestion are much welcome as comments, answers, and pull requests!欢迎任何进一步的代码改进建议作为评论、答案和拉取请求!

Update 27/09/20: I just realized VS Code intellisense is not showing type information for the store instance after passing it from useStore function, when used inside component. 27 年 9 月 20 日更新:我刚刚意识到 VS Code 智能感知在从useStore函数传递 store 实例后没有显示它的类型信息,当在组件内部使用时。 Works if imported in .ts file.如果在.ts文件中导入,则有效。

I have published a package to help with using Vuex 4 with Typescript.我发布了一个包来帮助将 Vuex 4 与 Typescript 一起使用。 It's not perfect, but it covers 95% of the issues we had.它并不完美,但它涵盖了我们遇到的 95% 的问题。 Any feedback would be appreciated.对于任何反馈,我们都表示感谢。

It requires TS 4.1+ for the template literal support.它需要 TS 4.1+ 来支持模板文字。

https://www.npmjs.com/package/typed-vuex-wrapper https://www.npmjs.com/package/typed-vuex-wrapper

FWIW the initial implementation was highly inspired by your start on modules, but expands that, and supports mapState/mapActions, etc with fully typed access. FWIW 最初的实现受到了您对模块的启发,但对其进行了扩展,并支持 mapState/mapActions 等具有完全类型化访问的功能。 Requires TS 4.1 for the template literal support to allow fully safe namespaced access without magic strings.需要 TS 4.1 的模板文字支持,以允许完全安全的命名空间访问,而无需魔法字符串。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM