假设有组件 MyComponent 在模块 MyModule 中。在 Angular 12 中,如果要将 MyComponent 移动到另一个模块中,需要将其从 MyModule 中删除并添加到新模块中,同时需要在新模块中导入 MyComponent 所在的原始模块。如下所示:
// 原始模块 MyModule 中的代码 import { MyComponent } from './my-component/my-component.component';
@NgModule({ declarations: [ MyComponent ], ... }) export class MyModule { }
// 新模块 NewModule 中的代码 import { MyModule } from './path/to/my-module.module'; import { MyComponent } from './my-component/my-component.component';
@NgModule({ declarations: [ MyComponent ], imports: [ MyModule ], ... }) export class NewModule { }
在新模块中,导入 MyModule 并将其添加到 imports 数组中。这样做是为了确保 MyComponent 所需的任何依赖项都可用。