Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
766 views
in Technique[技术] by (71.8m points)

angular - How to Access a component declared in parent module

Angular component does not recognize a component declared in the module its declared in. I am able to use the CommentsComponent in the ChildComponent, but not child module. I tried creating a shared module which was imported into the parent module and then exported to the child module. However that gave me a Firebase error. When i try to declare the CommentsComponent directly in the ChildModule It gives me this error:

CommentsComponent is part of the declarations of 2 modules: TabsModule and DocDetailModule!

however when I declare it in the parent module only or directly in the child module it does not recognize the component

I tried many different approaches but had no luck.

Parent module:

 imports:[childModule  // page component with module and routing]

    declarations: [
   ...
    ChildComponent //no module
    CommentsComponent,
   ... 
   
  ],`

Declaring the CommentsComponent in a module and exporting it to other modules:

// Comments.module.ts: (module being shared)

 @NgModule({
   imports: [
    CommonModule,
    ...
  ],
  declarations: [
    CommentsComponent,
  
],
exports: [CommentsComponent]

// child module that requires the component:

  @NgModule({
   imports: [
    CommonModule,
   ...
  ],
  declarations: [...],
  entryComponents: [...],
})

// parent module which encapsulates the child module and child component:

   @NgModule({
   imports: [
    ...
    CommentsModule,
    
    DocDetailModule,]

// the component works with firebase errors (((

Uncaught (in promise): FirebaseError: [code=invalid-argument]:     
Function Query.where() requires a valid third argument, but it was     
 undefined.

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

If you want to use a component which will be use in multiple modules so just create a shared module and then use that sharedModule at every place you want to use that generic component. Just create a shared module and generate commentsComponent in that like this

directory structure

shared(module)
  components
    commentsComponent
      comments.component.ts
      comments.component.html
  share.module.ts

and inside of shared.module.ts just do this

declarations: [CommentsComponent]
exports: [commentsComponent]

Include your shared components in exports array so that you can access in your any module.

Now when you want to use your comments.component let say in your module1 so just do this in your module1.module.ts

import { SharedModule } from 'path-to-shared-module';

imports : [SharedModule]

In this way you can use your shared components in multiple modules of your app.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...