资讯专栏INFORMATION COLUMN

Vue2.5+ Typescript 引入全面指南 - Vuex篇

DataPipeline / 640人阅读

摘要:引入全面指南篇系列目录引入全面指南引入全面指南篇前言正是我下决心引入的核心痛点。其中,可以通过建立辅助函数形式,简单绕开。只是类型均为建议不使用,以明确指定类型及调用可通过上述下辅助函数,手动开启类型推导及类型推导,暂时只能手动指定。

Vue2.5+ Typescript 引入全面指南 - Vuex篇

系列目录:

Vue2.5+ Typescript 引入全面指南

Vue2.5+ Typescript 引入全面指南 - Vuex篇

前言

Vuex 正是我下决心引入Typescript的核心痛点。与 vuex 相关的代码中,到处充斥着此般写法:

再加上vuex的 dispatch/commit 并非直接引用代码,而是是通过一个string类型的 type 来标记,如此一来上图中写法,如果想查看 payload 的具体内容,甚至不能借助于编辑器的查找定义,只能手动去切代码查看!简直苦不堪言。

而借助于 typescriptinterface 接口,至少可以简化成如下效果:

这么写一样麻烦,不是还需要记类似PostLoginParams的Interface类型?这简单,建个辅助函数就是:

编辑器里一个 Ctrl + 空格payload里有哪些参数就全出来,再也不用去一遍遍翻代码,效率直线提升!

现状概述

截至当前2017年11月,Vuex对Typescript的支持,仍十分薄弱,官方库只是添加了一些.d.ts声明文件,并没有像vue 2.5这样内置支持。

第三方衍生库 vuex-typescript, vuex-ts-decorators, vuex-typex, vuex-class等等,我个人的总结,除了vuex-class外,基本都存在侵入性太强的问题,引用不算友好。而vuex-class提供的功能其实也是薄薄一层,并不能解决核心痛点。因此,需要手动添加辅助的地方,其实颇多。

核心痛点:每次调用 this.$store.dispatch / this.$store.commit / this.$store.state/ this.$store.getters 都会伴随着类型丢失。

其中,dispatch/commit 可以通过建立辅助函数形式,简单绕开。 state/getters 没有太好办法,只能手动指定,若觉得麻烦,可以全都指成 any,等官方支持。官方动态见此 issue

动手改造第一步:从 shopping-cart 示例搬运代码

</>复制代码

  1. 以下示例基于 vuex 官方 examples 中最复杂的一个 shopping-cart,
    改造后的完整代码见 vue-vuex-typescript-demo

准备工作:

shopping-cart代码复制至项目目录下

.js文件统一重命名为.ts

currency.js/api/shop.js/components/App.vue等外围文件的ts改造

npm i -D vuex 添加依赖

详细步骤这里略去,参照 代码库 即可

动手改造第二步:State改造

用到state变量的地方实在太多,不仅store目录下 action/getter/mutation 均有可能需要,甚至在 .vue 文件里,mapState也有引用,因此我个人总结的一套实践:

store/modules下的每个子模块,均维护自己名为 State 的 Interface 声明

store/index.ts 文件中,汇总各子模块,维护一个总的State声明

store/modules 下文件举例:

</>复制代码

  1. // ./src/store/modules/cart.ts
  2. interface Shape {
  3. id: number
  4. quantity: number
  5. }
  6. export interface State {
  7. added: Shape[]
  8. checkoutStatus: "successful" | "failed" | null
  9. }
  10. // initial state
  11. // shape: [{ id, quantity }]
  12. const state: State = {
  13. added: [],
  14. checkoutStatus: null
  15. }
  16. // 需引用state的地方举例:
  17. const getters = {
  18. checkoutStatus: (state: State) => state.checkoutStatus
  19. }

store/index.ts 文件总 State 举例:

</>复制代码

  1. // ./src/store/index.ts
  2. import { State as CardState } from "./modules/cart"
  3. import { State as ProductsState } from "./modules/products"
  4. export interface State {
  5. cart: CardState,
  6. products: ProductsState
  7. }

State 引用示例:

</>复制代码

  1. // ./src/store/getters.ts
  2. import { State } from "./index"
  3. const cartProducts: Getter = (state: State) => {
  4. return state.cart.added.map(shape => {
  5. // 此处shape自动推导出Shape类型
  6. // ... 详见源码
  7. })
  8. }

如此,所有直接引用 state 的地方,均可启用类型推导

动手改造之 Mutation

Mutation 对应 store.commit 命令,常见写法:

</>复制代码

  1. const mutations = {
  2. [types.ADD_TO_CART] (state, { id }) {
  3. // ...
  4. }
  5. }

state 上步已处理{ id }payload 参数,即为开篇介绍类型缺失的重灾区。

我的一套个人实践:

store/modules 下的子模块文件,为自己的mutations 维护 payload Interface声明

子模块共用 payload(多个模块响应同一 commit 等),在 store/index.ts 中统一维护

新建文件 store/dispatches.ts 文件,为每一个直接调用的带参commit维护辅助函数,以应用类型推导

子模块 payload 声明举例:

</>复制代码

  1. // ./src/store/modules/products.ts
  2. import { Product, AddToCartPayload } from "../index"
  3. export interface ProductsPayload {
  4. products: Product[]
  5. }
  6. const mutations = {
  7. [types.RECEIVE_PRODUCTS] (state: State, payload: ProductsPayload) {
  8. state.all = payload.products
  9. },
  10. [types.ADD_TO_CART] (state: State, payload: AddToCartPayload) {
  11. const product = state.all.find(p => p.id === payload.id)
  12. // ...
  13. }
  14. }
  15. // mutations调用举例:
  16. const actions = {
  17. getAllProducts (context: ActionContextBasic) {
  18. shop.getProducts((products: Product[]) => {
  19. const payload: ProductsPayload = {
  20. products
  21. }
  22. context.commit(types.RECEIVE_PRODUCTS, payload)
  23. })
  24. }
  25. }

store/index.ts文件公共 payload 声明举例:

</>复制代码

  1. // ./src/store/index.ts
  2. export interface AddToCartPayload {
  3. id: number
  4. }

store/dispatches.ts文件,commit辅助函数,参见下步同文件dispatch辅助函数

动手改造之 Action

Action 对应 store.dispatch 命令,常见写法:

</>复制代码

  1. const actions = {
  2. checkout ({ commit, state }, products) {
  3. // ...
  4. }
  5. }

其中第二个参数productspayload 参数,用法同上步 Mutationpayload 参数,不再赘述。

第一个参数{ commit, state }context参数,vuexd.ts 提供有类型 ActionContext,用法如下:

</>复制代码

  1. import { ActionContext } from "vuex"
  2. const actions = {
  3. checkout (context: ActionContext, products: CartProduct[]) {
  4. context.commit(types.CHECKOUT_REQUEST)
  5. // ...
  6. }
  7. }

ActionContext 传入两个大部分Action根本用不到的参数,才能得到需要的dispatch, commit,在我看来,难用至极。

个人更喜欢如下写法:

</>复制代码

  1. const actions = {
  2. checkout (context: { commit: Commit, state: State }, products: CartProduct[]) {
  3. context.commit(types.CHECKOUT_REQUEST)
  4. // ...
  5. }
  6. }

Action payload 改造参见步骤 Mutation,不再赘述。

store/dispatches.ts文件,dispatch辅助函数:

</>复制代码

  1. // ./src/store/dispatches.ts
  2. import store, { CartProduct, Product } from "./index"
  3. export const dispatchCheckout = (products: CartProduct[]) => {
  4. return store.dispatch("checkout", products)
  5. }

.vue文件调用举例:

</>复制代码

  1. // ./src/components/Cart.vue
  2. import { dispatchCheckout } from "../store/dispatches"
  3. export default Vue.extend({
  4. methods: {
  5. checkout (products: CartProduct[]) {
  6. // this.$store.dispatch 写法可用,但不带类型推导
  7. // this.$store.dispatch("checkout", products)
  8. dispatchCheckout(products) // 带有类型智能提示
  9. }
  10. }
  11. })
动手改造之 Getter

Getter常见写法:

</>复制代码

  1. const getters = {
  2. checkoutStatus: state => state.checkoutStatus
  3. }

需要改的不多,state 加上声明即可:

</>复制代码

  1. const getters = {
  2. checkoutStatus: (state: State) => state.checkoutStatus
  3. }
动手改造之独立的 Mutations/Actions/Getters 文件

独立文件常规写法:

</>复制代码

  1. // ./src/store/getters.js
  2. export const cartProducts = state => {
  3. return state.cart.added.map(({ id, quantity }) => {
  4. const product = state.products.all.find(p => p.id === id)
  5. return {
  6. title: product.title,
  7. price: product.price,
  8. quantity
  9. }
  10. })
  11. }

引用:

</>复制代码

  1. // ./src/store/index.js
  2. import * as getters from "./getters"
  3. export default new Vuex.Store({
  4. getters
  5. })

typescript下均需改造:

</>复制代码

  1. // ./src/
  2. import { GetterTree, Getter } from "vuex"
  3. import { State } from "./index"
  4. const cartProducts: Getter = (state: State) => {
  5. return state.cart.added.map(shape => {
  6. // ...
  7. })
  8. }
  9. const getterTree: GetterTree = {
  10. cartProducts
  11. }
  12. export default getterTree

</>复制代码

  1. Actions/Mutations 文件改造同上,类型换成 ActionTree, Action, MutationTree, Mutation即可

引用:

</>复制代码

  1. // ./src/store/index.js
  2. import getters from "./getters"
  3. export default new Vuex.Store({
  4. getters
  5. })

原因是vuex定义,new Vuex.Store参数类型 StoreOptions 如下:

</>复制代码

  1. export interface StoreOptions {
  2. state?: S;
  3. getters?: GetterTree;
  4. actions?: ActionTree;
  5. mutations?: MutationTree;
  6. modules?: ModuleTree;
  7. plugins?: Plugin[];
  8. strict?: boolean;
  9. }

于是,独立Gettes/Actions/Mutations文件,export 必须是GetterTree/ActionTree/MutationTree类型

动手改造之 .vue 文件调用

传统写法全部兼容,只需 mapState为state添加类型 (state: State) => state.balabal 等很少改动即可正常运行。只是类型均为 any

建议不使用 mapState / mapGetters / mapActions / mapMutations,以明确指定类型

dispatchcommit 调用可通过上述 store/dispatches.ts 下辅助函数,手动开启类型推导

stategetters 类型推导,暂时只能手动指定。自动推导,估计得等官方内置支持了。

完整调用示例:

</>复制代码

  1. // ./src/components/ProductList.vue
  2. import Vue from "vue"
  3. // import { mapGetters, mapActions } from "vuex"
  4. import { Product } from "../store"
  5. import { dispatchAddToCart } from "../store/dispatches"
  6. export default Vue.extend({
  7. computed: {
  8. // ...mapGetters({
  9. // products: "allProducts"
  10. // })
  11. products (): Product[] {
  12. return this.$store.getters.allProducts
  13. }
  14. },
  15. methods: {
  16. // ...mapActions([
  17. // "addToCart"
  18. // ])
  19. addToCart (p: Product) {
  20. dispatchAddToCart(p)
  21. }
  22. },
  23. created () {
  24. this.$store.dispatch("getAllProducts")
  25. }
  26. })
vue-class-component + vuex-class 组件式写法

如果觉得以上废弃 mapState / mapGetters 后的写法繁琐,可引入vue-class-component + vuex-class,开启组件式写法

vue-class-component,vue官方维护,学习成本低

vuex-class,作者 ktsn,vuex及vue-class-component贡献排第二(第一尤雨溪了)的活跃开发者,质量还是有保障的

引入这俩依赖后,须在 tsconfig.json 添加配置:

</>复制代码

  1. {
  2. "compilerOptions": {
  3. // 启用 vue-class-componentvuex-class 需要开启此选项
  4. "experimentalDecorators": true,
  5. // 启用 vuex-class 需要开启此选项
  6. "strictFunctionTypes": false
  7. }
  8. }

Component 写法示例:

</>复制代码

  1. import Vue from "vue"
  2. import { Product } from "../store"
  3. // import { dispatchAddToCart } from "../store/dispatches"
  4. import Component from "vue-class-component"
  5. import { Getter, Action } from "vuex-class"
  6. @Component
  7. export default class Cart extends Vue {
  8. @Getter("cartProducts") products: CartProduct[]
  9. @Getter("checkoutStatus") checkoutStatus: CheckoutStatus
  10. @Action("checkout") actionCheckout: Function
  11. get total (): number {
  12. return this.products.reduce((total, p) => {
  13. return total + p.price * p.quantity
  14. }, 0)
  15. }
  16. checkout (products: CartProduct[]) {
  17. // dispatchCheckout(products)
  18. this.actionCheckout(products)
  19. }
  20. }
总结

在现阶段 vuex 官方未改进 typescript 支持下,用 typescript 写 vuex 代码,的确有些繁琐,而且支持也称不上全面,不过,总比没有强。哪怕都用 any,也能借助智能提示减轻一些代码翻来翻去的痛苦。

至于再进一步更完美的支持,等官方更新吧。

完整代码

见 Github 库:vue-vuex-typescript-demo

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/89543.html

相关文章

  • Vue2.5+ Typescript 引入全面指南

    摘要:引入全面指南系列目录引入全面指南引入全面指南篇写在前面写这篇文章时的我,使用经验三个多月,完全空白,花了大概三个晚上把手头项目迁移至,因此这篇文章更像个入门指引。见文章引入全面指南篇完整代码见库,分支为整合示例,分支为不含的基础示例。 Vue2.5+ Typescript 引入全面指南 系列目录: Vue2.5+ Typescript 引入全面指南 Vue2.5+ Typescrip...

    liangzai_cool 评论0 收藏0
  • Vue2.5+迁移至Typescript指南

    摘要:迁移至指南为什么要迁移至本身是动态弱类型的语言,这样的特点导致了代码中充斥着很多的报错,给开发调试和线上代码稳定都带来了不小的负面影响。可行性因为是的超集,不会阻止的运行,即使存在类型错误也不例外,这能让你的逐步迁移至。 Vue2.5+迁移至Typescript指南 为什么要迁移至Typescript Javascript本身是动态弱类型的语言,这样的特点导致了Javascript代...

    Ilikewhite 评论0 收藏0
  • Vue2.5+迁移至Typescript指南

    摘要:迁移至指南为什么要迁移至本身是动态弱类型的语言,这样的特点导致了代码中充斥着很多的报错,给开发调试和线上代码稳定都带来了不小的负面影响。可行性因为是的超集,不会阻止的运行,即使存在类型错误也不例外,这能让你的逐步迁移至。 Vue2.5+迁移至Typescript指南 为什么要迁移至Typescript Javascript本身是动态弱类型的语言,这样的特点导致了Javascript代码...

    wenshi11019 评论0 收藏0
  • 前方来报,八月最新资讯--关于vue2&3的最佳文章推荐

    摘要:哪吒别人的看法都是狗屁,你是谁只有你自己说了才算,这是爹教我的道理。哪吒去他个鸟命我命由我,不由天是魔是仙,我自己决定哪吒白白搭上一条人命,你傻不傻敖丙不傻谁和你做朋友太乙真人人是否能够改变命运,我不晓得。我只晓得,不认命是哪吒的命。 showImg(https://segmentfault.com/img/bVbwiGL?w=900&h=378); 出处 查看github最新的Vue...

    izhuhaodev 评论0 收藏0

发表评论

0条评论

最新活动
阅读需要支付1元查看
<