POV
POV

Reputation: 12015

How to use component in others?

I have a component TreeComponent. It has no module. Just component anf template.

How to use this componnt in other components?

When I add component to declaration section of another component:

@NgModule({
declarations: ["TreeComponent"]
});

I get an error:

Type TreeComponent is part of the declarations of 2 modules

Upvotes: 0

Views: 58

Answers (1)

SiddAjmera
SiddAjmera

Reputation: 39462

You're getting this error because you have added TreeComponent to the declarations array of two Angular Modules.

Instead of doing that just export TreeComponent from a Module

@NgModule({
  declarations: [TreeComponent],
  exports: [TreeComponent]
})
export class MyCustomModule;

And then add your Module to the imports array of any other module in which you want to use this TreeComponent

@NgModule({
  imports: [MyCustomModule, ...],
  ...
})
export class MyOtherModule;

and

@NgModule({
  imports: [MyCustomModule, ...],
  ...
})
export class SomeOtherModule;

Here's a Sample Code Example for your ref.

As of now, I've just added the CustomComponentsModule to the imports array of the AppModule. But you can also add CustomComponentsModule to the imports array of any Angular Module you want to use the TreeComponent in.

Upvotes: 1

Related Questions