在 Angular 中,如果在 NgModule.imports 中引入了未使用的模块,不会产生任何错误,但是会导致应用程序运行时间的性能影响。
因此,为了防止这种影响,我们最好应该仅在需要时才引入所需的模块。
下面是示例代码:
// app.module.ts
import { NgModule } from '@angular/core'; import { BrowserModule } from '@angular/platform-browser'; import { FormsModule } from '@angular/forms';
import { AppComponent } from './app.component';
@NgModule({ imports: [ BrowserModule, FormsModule, // 只有在需要时才引入特定的模块 // ExampleModule, ], declarations: [ AppComponent ], bootstrap: [ AppComponent ] }) export class AppModule { }