Skip to content
| Marketplace
Sign in
Visual Studio Code>Other>dva-generate-dispatch-typeNew to Visual Studio Code? Get it now.
dva-generate-dispatch-type

dva-generate-dispatch-type

姚兴林

|
452 installs
| (1) | Free
提取dva中model的effects和reducer中的参数类型,方便在dispatch的时候调用
Installation
Launch VS Code Quick Open (Ctrl+P), paste the following command, and press enter.
Copied to clipboard
More Info

dva-generate-dispatch-type

监听 dva 中 model ts 文件,自动生成 dispatch 需要的类型

使用仅需 3 步

image

1: 在 vscode 插件市场搜索 dva-generate-dispatch-type,并安装

2: 在 model TS 文件中右击,生成模版

3: 在保存的时候会自动生成对应的 dispatch 类型

Extension Settings

This extension contributes the following settings:

  • 提取 Payload 中数据的类型的正则表达式,数据类型添加括号分组,默认配置如下
  • dva-generate-dispatch-type.payloadRegExp: Payload<(.*)> image

使用体验

image

自定义配置

1: 在根目录下添加 exportModelTypeTemplate.ejs 渲染模版

image 在项目根目录下创建 exportModelTypeTemplate.ejs 模板文件,采用swig-templates渲染引擎

默认内容模版如下

{% if namespace %}
/**
 * model【{{namespace}}】 中reducer和effects对应类型
 */
export declare namespace dispatchType{{firstUpperCase(namespace)}} {
    export interface SetState{% if !rootStateType %}<T>{% endif %} {
        type: '{{namespace}}/setState';
        payload: Partial<{{rootStateType||"T"}}>;
    }
    export interface Reset{
        type: '{{namespace}}/reSet';
    }
    {% for method in methodObject %}
    {% if method.doc.length!==0 %}
    /**
{% for doc in method.doc %}     * {{doc}}
{% endfor %}     */{% endif %}
    export interface {{firstUpperCase(method.methodName)}} {
        type: '{{namespace}}/{{method.methodName}}';
        {% if method.type %}payload:{{formatPayloadByReg(method.type)}}{% endif %}
    };
    {% endfor %}
}
{% endif %}

数据类型如下


/**
 *返回dva model 的类型
 *
 * @export
 * @interface IDvaModelType
 */
export interface IDvaModelType {
    [key: string]: {
        /**
         *文本注释数组
         *
         * @type {string[]}
         */
        doc: string[];
        /**
         * payload的类型
         *
         * @type {string}
         */
        type: string;
        /**
         *函数名称
         *
         * @type {string}
         */
        methodName: string;
    };
}

export interface IModelType {
    /**
     *dva model 的命名空间
     *
     * @type {string}
     * @memberof IModelType
     */
    namespace: string;
    methodObject: IDvaModelType;
    rootStateType: string;
}

额外添加了 2 个工具函数

/**
 *将首字母专为大写
 * @example
 * firstUpperCase('abc');// Abc
 * @export
 * @param {string} str
 * @returns
 */
export function firstUpperCase(str: string) {}
/**
 * 提取字符串中匹配到的正则表达式第一个分组括号里的值后的值,如果没有返回目标字符串
 * @example
 * getFormatPayloadByReg('Payload<IUser>','Payload<(.*)>') ; // IUser;
 * getFormatPayloadByReg('IUser','Payload<(.*)>') ; // IUser;
 * @export
 * @param {string} target 被转换的值
 * @param {string} strReg 要提取的正则表达式,要加括号
 * @returns
 */
export function getFormatPayloadByReg(target: string, strReg: string) {}

Known Issues

Release Notes

0.0.3

修改 reSet 单词错误的问题

0.0.2

修改文档

0.0.1

第一次发布


For more information

  • Visual Studio Code

Enjoy!

  • Contact us
  • Jobs
  • Privacy
  • Manage cookies
  • Terms of use
  • Trademarks
© 2025 Microsoft