[英]Share angular components between different modules
Can i share component from module 1 in module 2 and share component from module2 in module 1?我可以在模块 2 中共享模块 1 中的组件并在模块 1 中共享模块 2 中的组件吗?
I have two modules, module 1 is copy_role and module 2 is assign_role.我有两个模块,模块 1 是 copy_role,模块 2 是 assign_role。
Module 1 copy_role has two components.模块 1 copy_role 有两个组件。
component 1 is copy-user-role and组件 1 是复制用户角色,并且
component 2 is copy-user-module组件 2 是复制用户模块
Module 2 assign_role has one component ie assign-user-role模块 2 assign_role 有一个组件,即 assign-user-role
I am able to share component assign-user-role in module1 copy_role, same way i would like to use component 2 copy-user-module present in module1 in module2 assign_role, i am getting below error我能够在 module1 copy_role 中共享组件 assign-user-role,同样我想在 module2 assign_role 中使用 module1 中存在的组件 2 copy-user-module,我遇到以下错误
Error: Module build failed (from ./node_modules/@ngtools/webpack/src/ivy/index.js):
Error: Maximum call stack size exceeded
at C:\code\UI_APPLICATIONS_WS\test\test_sec\node_modules\@ngtools\webpack\src\ivy\loader.js:77:18
× Failed to compile.
Is this the right way or am i doing any mistake here?这是正确的方法还是我在这里做错了?
The "Maximum call stack size exceeded" happens because you try to import module1
in module2
and vice-versa.发生“超出最大调用堆栈大小”是因为您尝试在模块 2 中导入
module1
1, module2
。 Angular will try to import modules in an infinite loop resulting with this error, as I could reproduce in this StackBlitz Angular 将尝试在导致此错误的无限循环中导入模块,因为我可以在此StackBlitz中重现
Given that you cannot declare a component in 2 modules, there are 2 usual solutions:鉴于您不能在 2 个模块中声明组件,通常有 2 种解决方案:
For example, with the mono-component module:例如,对于单组件模块:
Define the component:定义组件:
@Component({
selector: 'app-shared-component',
template: '<div>Shared Component</div>',
})
export class MySharedComponent {}
Declare it in a module and export it:在模块中声明它并导出它:
@NgModule({
imports: [],
declarations: [MySharedComponent],
exports: [MySharedComponent],
})
export class MySharedComponentModule {}
Then you can import it in 2 different modules:然后你可以将它导入到 2 个不同的模块中:
@Component({
selector: 'app-component-1',
template: '<h1>Component 1</h1><app-shared-component></app-shared-component>',
})
export class MyComponent1 {}
@NgModule({
imports: [MySharedComponentModule],
declarations: [MyComponent1],
exports: [MyComponent1],
})
export class Module1 {}
@Component({
selector: 'app-component-2',
template: '<h1>Component 2</h1><app-shared-component></app-shared-component>',
})
export class MyComponent2 {}
@NgModule({
imports: [MySharedComponentModule],
declarations: [MyComponent2],
exports: [MyComponent2],
})
export class Module2 {}
Then import those 2 modules and use them:然后导入这两个模块并使用它们:
@NgModule({
imports: [BrowserModule, FormsModule, Module1, Module2],
declarations: [AppComponent],
bootstrap: [AppComponent],
})
export class AppModule {}
I cannot answer concerning your "Maximum call stack size exceeded at" error.我无法回答您的“最大调用堆栈大小超出”错误。
However, if you want to reuse the same component in multiple modules, you could create a new module called "SharedModules" and declare the components in it.然而,如果你想在多个模块中重用同一个组件,你可以创建一个名为“SharedModules”的新模块并在其中声明组件。
You can then import the "SharedModules" inside module1 and module2.然后您可以在 module1 和 module2 中导入“SharedModules”。 You will then have access to both component in module1 and module2.
然后您将可以访问 module1 和 module2 中的组件。
Here is the angular Doc concerning Sharing modules: Documentation这是有关共享模块的 angular 文档:文档
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.