Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular2 Component declared in multiple modules

Tags:

angular

My Angular2 RC6 application has two modules and I'm not sure how to declare a shared component.

I have a component named spinnerComponent that is used throughout the application. I defined it in app.modules.as:

@NgModule({
    imports: [BrowserModule, routing,RepairReturnModule],
    providers: [ ],
    declarations: [AppComponent,SpinnerComponent],
    bootstrap: [AppComponent]
})

Then in RepairreturnModule I define it again as:

@NgModule({
    imports: [CommonModule],
    declarations: [
        SpinnerComponent
    ],
    providers: []
})

As expected, I get:

Type SpinnerComponent is part of the declarations of 2 modules: RepairReturnModule and AppModule

I removed SpinnerComponent from the declaration in RepairreturnModule as then I get:

Unhandled Promise rejection: Template parse errors: Can't bind to 'isRunning' since it isn't a known property of 'spinner-component'. 1. If 'spinner-component' is an Angular component and it has 'isRunning' input, then verify that it is part of this module. 2. If 'spinner-component' is a Web Component then add "CUSTOM_ELEMENTS_SCHEMA" to the '@NgModule.schema' of this component to suppress this message. ... which indicated that it is not declared.

What am I missing?

like image 789
Don Chambers Avatar asked Sep 08 '16 20:09

Don Chambers


People also ask

Can we declare a component in multiple modules in Angular?

We can list components, directives, and pipes, which are part of the module, in the "declarations" array. We can import other modules by adding them to the "imports" array. We can list down all the services which are part of our module in the "providers" array.

How do you declare a component in two modules?

In this scenario, create another shared module in that import all the component which is being used in multiple module. In shared component. declare those component. And then import shared module in appmodule as well as in other module where you want to access.

Can we have multiple NgModule in Angular?

Yes you can split your application into modules.


2 Answers

Add exports: [SpinnerComponent] to your app.modules NgModules decorator.

Reference: https://angular.io/docs/ts/latest/guide/architecture.html,

``` NgModule is a decorator function that takes a single metadata object whose properties describe the module. The most important properties are:

...

exports - the subset of declarations that should be visible and usable in the component templates of other modules.

...

```

like image 179
sdemoor Avatar answered Sep 21 '22 11:09

sdemoor


Seeing the full code would be beneficial but anyway...

You can try to move spinner back to repair module and import it from there in the app module. I use separate (in your case it would be third) 'shared' module where common functionality sits so every other module can import it from there.

like image 22
rook Avatar answered Sep 19 '22 11:09

rook