【问题标题】:Creating new module using Angular schematics使用 Angular 原理图创建新模块
【发布时间】:2019-04-08 19:06:19
【问题描述】:

我们想使用原理图来生成一个功能模块,其中包含我们一遍又一遍地生成的组件。

我已经开始学习本教程:https://medium.com/@michael.warneke/merging-custom-angular-schematics-c14a303f63b6

但是当我创建 __name@dasherize@singularize__.module.ts 文件并尝试构建项目时,我得到了 TS 编译错误。

这是我的文件的样子:

import { ModuleWithProviders, NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';

@NgModule({
    imports: [
        CommonModule,
    ],
    declarations: [],
    providers: [],
    entryComponents: []
 })
export class <%= classify(name) %>Module {
    static forRoot(): ModuleWithProviders {
        return {
            ngModule: <%= classify(name) %>Module,
            providers: []
        };
     }
}

这是错误:

错误 TS6133:声明了“ModuleWithProviders”,但从未读取其值。

【问题讨论】:

    标签: angular angular-cli angular-schematics


    【解决方案1】:

    从您的帖子看来,这是一个编译时错误,即npm run build,而不是一个生成时错误,即ng new CustomSchematic --collection my-custom-schematic

    我看到的这种行为最常见的罪魁祸首是 Typescript 正在针对您的原理图资产运行其验证规则。在这种情况下,我希望您的tsconfig.json 具有以下属性和值"noUnusedLocals": true。将该属性设置为 false 可能不是一个好的长期策略,因为这意味着您的所有原理图应用程序代码也不再执行该验证规则。

    相反(以及我选择在我的projects 上执行的操作)是将以下属性添加到tsconfig.json 文件中:

    "exclude": [
        "src/*/files/**/*"
    ]
    

    这将正确排除原理图集合中每个原理图的files 文件夹下的所有资产。

    /src
      /ng-new
         /files
            custom-asset.ts
            custom-asset.html
         index.ts
         index_spec.ts
      /custom-service
         /files
            custom-asset.ts
         index.ts
         index_spec.ts
    

    我无法完全复制您的问题,所以如果您有公共回购链接,您可以分享它。但是,当我删除 tsconfig.json 中的 excludes 设置时,我会从编译到大量的 Typescript 错误,所以我有信心这是正确的方向。

    作为最后一个提示,如果您使用的是 VS 代码并显示了 Problems 面板,则可以使用 !src/*/files/**/* 文件路径应用相同的错误抑制策略。

    【讨论】:

      猜你喜欢
      • 2020-02-17
      • 2019-06-17
      • 1970-01-01
      • 2018-05-03
      • 2020-12-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-08-21
      相关资源
      最近更新 更多