- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
当使用 Schematics 在 Angular 中生成组件时,它会尝试将新生成的组件导入到 NgModule 中,这是强制性的,以便组件可以工作并节省时间。
但是,在创建我自己的原理图时,我无法找到正确的方法来执行相同的操作并将我的库导入到 app.component.ts 的 NgModule 中。使用 ng-add schematics 时,如何让 schematic 自动将其自己的条目添加到 NgModule 中?
原理图需要做两件事:
之前:
@NgModule({
declarations: [],
imports: [],
exports: []
})
export class appModule { }
之后:
import {library} from 'library';
@NgModule({
declarations: [],
imports: [library],
exports: []
})
export class appModule { }
我知道 angular devkit 中存在执行此操作的功能,因为组件的“官方”原理图等使用它。我该怎么做?
最佳答案
对于第一种情况,类似下面的代码可以帮助您:
export function addImportStatement(tree: Tree, filePath: string, type: string, file: string): void {
let source = getTsSourceFile(tree, filePath);
const importChange = insertImport(source, filePath, type, file) as InsertChange;
if (!(importChange instanceof NoopChange)) {
const recorder = tree.beginUpdate(filePath);
recorder.insertLeft(importChange.pos, importChange.toAdd);
tree.commitUpdate(recorder);
}
}
第二个暂时不记得了,你可以查一下angular-cli schematics存储库,以便查看它是如何为模块之类的东西完成的。
干杯!
关于Angular Schematics - 添加库到 NgModule 导入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59304074/
它工作得很好,突然它在运行 ng serve 时开始给我下面的异常。我也没有做任何最近的升级或依赖项添加。 有人能帮我解决一下吗? ERROR in Error: NgModule is not an
关注此discussion ,我遇到了同样的问题:我无法在多个模块中导入 angular2-materialize 的指令,而不会出现错误消息“Type X is part of the statem
我想知道是创建一个包含我所有 Angular 2 代码的单个模块更好,还是将所有内容拆分到多个模块更好。例如,我正在使用 Angular 2 创建我的博客。所以我有一个“文章列表”组件和一个“文章”组
我正在关注这个 tutorial拥有我的应用程序的架构。 要提供更多信息,请考虑 A作为 appModule,和 B是另一个主要模块。现在我想在 B 的 NgModule 中加载其他模块( 有许多其
我正在尝试此链接中的所有解决方案: https://stack247.wordpress.com/2018/09/07/angular-all-possible-solutions-for-no-ng
当我使用 CLI 创建新组件时,我在这篇文章的主题中遇到错误:ng g c my-component --project=my-project(没有创建组件) 我看过其他帖子有相同的错误消息,但没有一
ERROR in node_modules/@angular/material/dialog/dialog.d.ts:142:22 -error NG6002: Appears in the NgMo
我刚刚从 Angular 8.2 更新到 9。 如果我禁用 ivy 一切正常,但是当它启用时,我收到了很多似乎有点模糊的错误,至少我在谷歌搜索时找不到任何结果。 我很确定我已经完成了更新所需的一切,即
第一次使用firestore,我收到了这个错误。根据我的研究,这似乎是 Ivy 的问题。我没有很多修改 tsconfig.app.json 的经验,这是我被指出的方向,遵循其他答案。 我能够从原始项目
您好,我刚刚使用简单的 CLI 从 CLI 创建了一个新项目 ng new frontend 一旦我进入我的项目,命令: cd frontend/src/app 并尝试像这样生成一个新组件 ng ge
我在应用程序上有三个模块,其中一个是主模块,用于其他两个模块。 当我启动应用程序并直接转到导入主模块的这些模块之一时,浏览器控制台中会显示此错误: core.js:4117 ERROR Error:
我有使用 ngx-translate 中的 TranslateModule 的模块(问题不是关于这个库,而是关于 Angular)。它看起来像这样: @NgModule({ ... impor
我想创建一个与图表相关的模块并将其写在其他地方。 模块声明源码如下。 图表.module.ts import { NgModule, ModuleWithProviders } from '@angu
我正在通过刚买的一本书学习 angular 2(成为 angular 2 的忍者)。 我也在同时浏览官方文档,但是在从官方网站进行快速入门时,我看到他们在谈论 @NgModule 而书中没有提到它。
我有这个 NgModule: @NgModule({ imports: [ CommonModule ], exports: [ SP21Loa
所以我现在正在学习 angular2 并阅读有关模块的 ngbook2。模块包含组件,但也可以导入具有公共(public)组件的不同模块。 问题是:模块组件的范围是什么(在这个范围内是应用程序的一部分
所以我创建了一个组件 示例组件 并在 的模块中声明它不是在 app.module 中列出。 接下来我要声明组件 示例组件 在一个模块中 是 在 app.module 中列出。我该怎么做呢?如果我简单地
我使用 angular 9,我想做延迟加载,我做 app-routing { path: '', loadChildren: () => import("./components/login/
我不明白为什么当我有了新的提供商时,我一次又一次地遇到这个错误。当我有任何其他提供商(如地理定位等)时,我遇到此错误: Error: Uncaught Error: Invalid provider
我有一个 Angular 应用程序,但我不明白编译器告诉我为什么应用程序无法构建的原因。这些是我从编译器收到的错误: ERROR in src/app/list-items/list-items.co
我是一名优秀的程序员,十分优秀!