我想创建一个自定义的角度原理图,它可以接受动作名称的集合。然后,我将为用户提供的每个操作名称生成3个ngrx操作。
例如,我想创建一个可以像这样调用的示意图:
ng g my-collection:my-schematic --actions=GetById,GetByFirstName然后我将为GetById、GetByIdSuccess、GetByIdError、GetByFirstName、GetByFirstNameSuccess、GetByFirstNameError生成代码。
问题是我只看到angular schematics接受单个值作为输入参数。有人知道如何在自定义的angular原理图中处理集合吗?
发布于 2019-01-29 23:18:16
你可以关注这个博客,它将教你如何创建自己的schematics项目:
https://blog.angular.io/schematics-an-introduction-dc1dfbc2a2b2
在文件collection.json中生成逻辑示意图项目后,可以扩展@ngrx/schematics
{
...
"extends": ["@ngrx/schematics"],
}然后使用ngrx schematics生成3个动作,如下所示:
externalSchematic('@ngrx/schematics', 'action')发布于 2019-01-30 04:38:26
我还没有找到一个很好的例子来说明如何将字符串数组转换为原理图参数,但我找到了一个解决方法。我所做的是有一个简单的字符串输入参数,该参数是一致分隔的(我使用,来分隔值)。以下是我的方案:
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));如果你知道处理这些的更好的方法,请分享。谢谢!
发布于 2020-05-24 05:07:28
您需要多次传递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[]
}https://stackoverflow.com/questions/54410585
复制相似问题