【问题标题】:Is it possible to generate multiple Angular components using single angular-cli command like ng generate component comp1, comp2, comp3?是否可以使用单个 angular-cli 命令(如 ng generate component comp1、comp2、comp3)生成多个 Angular 组件?
【发布时间】:2018-12-13 13:33:35
【问题描述】:

我需要创建十个 Angular 组件,例如 comp1、comp2、comp3、comp4、...、comp10。 所以我需要为每个组件(如 ng g c comp1、ng g c comp2 等)执行十次相同的命令。

是否有使用单个 angular-cli 命令(如 ng g c comp1、comp2、comp3)生成多个角度分量的选项

【问题讨论】:

  • Angular 尚不支持 .. 将来您可能会获得此功能。
  • 在 github github.com/angular/angular/issues/24761 上创建了相同的功能请求,敬请期待更新
  • 创建空组件后,您将编写一些代码,对其进行测试和验证。然后根据需要继续创建依赖组件。如何/为什么会出现对某些确定性组件集的需求
  • 只在你的shell上写循环?
  • @NitinSingh 同意你的观点,但在应用程序开始时,最需要常见的组件骨架,如主页、工具栏、页脚或类似登录、注册。

标签: angular angular-cli angular-cli-v6


【解决方案1】:

嗯,到目前为止,Angular CLI 还没有任何东西可以创建多个组件。但这可以在任何 shell 中轻松完成。

在你的 bash shell 中运行这个命令 -

for i in comp1 comp2; do ng g c "${i}"; done

特别感谢Ingo Bürk的回答

【讨论】:

  • 所以你从 Github 复制了我的确切答案,甚至包括措辞,甚至没有提及?
  • @IngoBürk 是的,我已经发布了指向 GitHub 问题的链接,您可以在问题的 cmets 上查看。没关系,我也会更新答案:)
【解决方案2】:

Microsoft Windows 变体

在根目录中:

for %n in (component-name-1, component-name-2, ...) do ng g c %n

在模块中:

for %n in (component-name-1, component-name-2, ...) do ng g c module-name/%n

在模块中,没有测试:

for %n in (component-name-1, component-name-2, ...) do ng g c module-name/%n --skipTests=true

Angular - ng generate

Windows CMD FOR command reference

【讨论】:

    【解决方案3】:

    我刚刚创建了一个 bash 脚本

    createcomponent.sh

    下面的一小段代码

    #!/bin/bash
    while [ "$1" != "" ]; do
        npx ng g c "components/$1"
        shift
    done
    

    然后就叫它

     createcomponent.sh comp1 comp2 comp3 ...
    

    如果有人使用 Windows,也可以使用 .bat 完成相同的操作。 npx 如果使用全局安装的 Angular,则不需要关键字

    【讨论】:

      【解决方案4】:

      如果您不想在此处创建脚本,这是一种可行的技巧

      1. 打开记事本或任何文本编辑器
      2. 使用如下语法编写组件名称:
      ng g c 组件/联系人 ng g c 组件/登录 ng g c 组件/注册 ng g c 组件/仪表板
      1. 在您的 CLI 中复制并粘贴(我使用了 Git bash)。它将一一执行,没有任何忙碌。您还可以添加特定于组件的参数

      【讨论】:

        猜你喜欢
        • 2017-04-05
        • 2023-02-01
        • 1970-01-01
        • 2020-02-05
        • 2018-03-10
        • 2018-12-01
        • 2018-12-02
        • 2021-05-20
        • 2020-05-16
        相关资源
        最近更新 更多