0

我想创建一个可以接受一组动作名称的自定义角度示意图。然后,我将为用户提供的每个操作名称生成 3 个 ngrx 操作。

例如,我想创建一个可以像这样调用的原理图:

ng g my-collection:my-schematic --actions=GetById,GetByFirstName

然后,我将为 GetById、GetByIdSuccess、GetByIdError、GetByFirstName、GetByFirstNameSuccess、GetByFirstNameError 生成代码。

问题是我只看到了将接受单个值作为输入参数的角度示意图。任何人都知道如何在自定义角度示意图中处理集合?

4

4 回答 4

0

如果您想直接从命令 args 中提取它们,您可以执行以下操作:

{
  "$schema": "http://json-schema.org/draft-07/schema",
  "$id": "Sample",
  "title": "Sample Schematic",
  "type": "object",
  "description": "Does stuff.",
  "additionalProperties": false,
  "properties": {
    "things": {
      "type": "array",
      "items": {
        "type": "string"
      },
      "$default": {
        "$source": "argv"
      },
      "description": "Things from the command-line args."
    }
  }
}

然后,当您运行原理图时,您可以执行以下操作:

schematics schematic-lib:sample stuff other-stuff more-stuff

在这种情况下,things属性将为['stuff', 'other-stuff', 'more-stuff']

编辑:请注意,required如果您不提供任何参数,架构中的值不会导致原理图失败。您需要在原理图中对该属性进行验证。

于 2021-05-28T19:38:16.897 回答
0

你可以关注这个博客,它将教你如何创建自己的原理图项目:

https://blog.angular.io/schematics-an-introduction-dc1dfbc2a2b2

在文件中生成原理图项目后,collection.json您可以扩展@ngrx/schematics

{
...
"extends": ["@ngrx/schematics"],
}

然后使用ngrx原理图生成3个这样的动作:

externalSchematic('@ngrx/schematics', 'action')
于 2019-01-29T15:18:16.360 回答
0

我还没有找到一个很好的例子来说明如何将字符串数组转换为原理图参数,但我找到了一种解决方法。我所做的是有一个始终分隔的简单字符串输入参数(我使用 , 来分隔值)。这是我的架构:

export interface Schema {
  name: string;
  path?: string;
  project?: string;
  spec?: boolean;
  actions: string;
  __actions: string[];
  store?: string;
}

我解析提供的操作参数并生成字符串数组 __actions 并在我的模板中使用该属性。这是我的 index.ts 中的一个片段:

export default function(options: ActionOptions): Rule {
  return (host: Tree, context: SchematicContext) => {
    options.path = getProjectPath(host, options);

    const parsedPath = parseName(options.path, options.name);
    options.name = parsedPath.name;
    options.path = parsedPath.path;
    options.__actions = options.actions.split(',');
    options.__actions = options.__actions.map(_a => classify(_a));

如果您知道处理这些的更好方法,请分享。谢谢!

于 2019-01-29T20:38:25.693 回答
0

您需要actions多次通过。

ng g my-collection:my-schematic --actions=GetById --actions=GetByFirstName

将参数定义为文件中的数组schema.json

    ...
    "actions": {
      "type": "array",
      "items": {
        "type": "string"
      },
      "description": "The name of the actions."
    },
    ...

也在你的schema.ts.

export interface Schema {
  actions: string[]
}
于 2020-05-23T21:07:27.667 回答