Import multiple Angular components via module
Asked Answered
I

4

17

My Angular project is growing so I want to keep my project clean.

I have one Angular component which depends on a nested Angular component.

Now I need two import statements to use these components which cannot work without each other.

As a solution I wanted to create a small Angular module which contains these two components and import the module into my main app.module.

After doing this I get an error which states that one of the components inside the small module is not recognized.

//app.module.ts
@NgModule({
    imports: [BrowserModule, HttpModule, DictaatModule],
    declarations: [AppComponent, DictatenComponent, FilePreviewComponent],
    bootstrap: [AppComponent]
})


//Dictaat.module.ts
import { DictaatComponent } from './dictaat.component';
import { DictaatEntryComponent } from './dictaat-entry.component';

@NgModule({
    imports: [BrowserModule, HttpModule],
    declarations: [DictaatComponent, DictaatEntryComponent],
})
export class DictaatModule{ }

My question: Is it a good practice to group multiple components into one module and is this already supported in Angular?

ps. Im working with Angular 2.0.0, not any RC's. My setup is comparable to the setup of the tour of heroes tutorial.

Edit: Source code https://github.com/Linksonder/Webdictaat/

Error msg:


Unhandled Promise rejection: Template parse errors:
Can't bind to 'dictaatName' since it isn't a known property of 'wd-dictaat'.
1. If 'wd-dictaat' is an Angular component and it has 'dictaatName' input, then verify that it is part of this module.
2. If 'wd-dictaat' is a Web Component then add "CUSTOM_ELEMENTS_SCHEMA" to the '@NgModule.schema' of this component to suppress this message.
 ("
    </div>
    <div class="col-md-3">
        <wd-dictaat [ERROR ->][dictaatName]="selectedDictaat.name" *ngIf="selectedDictaat">Loading dictaat...</wd-dictaat>
    </d"): DictatenComponent@21:20
'wd-dictaat' is not a known element:
1. If 'wd-dictaat' is an Angular component, then verify that it is part of this module.
2. If 'wd-dictaat' is a Web Component then add "CUSTOM_ELEMENTS_SCHEMA" to the '@NgModule.schema' of this component to suppress this message. ("
    </div>
    <div class="col-md-3">
        [ERROR ->]<wd-dictaat [dictaatName]="selectedDictaat.name" *ngIf="selectedDictaat">Loading dictaat...</wd-dicta"): DictatenComponent@21:8 ; Zone: <root> ; Task: Promise.then ; Value: Error: Template parse errors:(…) Error: Template parse errors:
Can't bind to 'dictaatName' since it isn't a known property of 'wd-dictaat'.
1. If 'wd-dictaat' is an Angular component and it has 'dictaatName' input, then verify that it is part of this module.
2. If 'wd-dictaat' is a Web Component then add "CUSTOM_ELEMENTS_SCHEMA" to the '@NgModule.schema' of this component to suppress this message.
 ("
    </div>
    <div class="col-md-3">
        <wd-dictaat [ERROR ->][dictaatName]="selectedDictaat.name" *ngIf="selectedDictaat">Loading dictaat...</wd-dictaat>
    </d"): DictatenComponent@21:20
'wd-dictaat' is not a known element:
1. If 'wd-dictaat' is an Angular component, then verify that it is part of this module.
2. If 'wd-dictaat' is a Web Component then add "CUSTOM_ELEMENTS_SCHEMA" to the '@NgModule.schema' of this component to suppress this message. ("
    </div>
    <div class="col-md-3">
        [ERROR ->]<wd-dictaat [dictaatName]="selectedDictaat.name" *ngIf="selectedDictaat">Loading dictaat...</wd-dicta"): DictatenComponent@21:8
    at TemplateParser.parse (http://localhost:3000/node_modules/@angular/compiler/bundles/compiler.umd.js:8530:21)
    at RuntimeCompiler._compileTemplate (http://localhost:3000/node_modules/@angular/compiler/bundles/compiler.umd.js:16905:53)
    at eval (http://localhost:3000/node_modules/@angular/compiler/bundles/compiler.umd.js:16828:85)
    at Set.forEach (native)
    at compile (http://localhost:3000/node_modules/@angular/compiler/bundles/compiler.umd.js:16828:49)
    at ZoneDelegate.invoke (http://localhost:3000/node_modules/zone.js/dist/zone.js:192:28)
    at Zone.run (http://localhost:3000/node_modules/zone.js/dist/zone.js:85:43)
    at http://localhost:3000/node_modules/zone.js/dist/zone.js:451:57
    at ZoneDelegate.invokeTask (http://localhost:3000/node_modules/zone.js/dist/zone.js:225:37)
    at Zone.runTask (http://localhost:3000/node_modules/zone.js/dist/zone.js:125:47)
Inconvenient answered 19/9, 2016 at 11:38 Comment(2)
post the error message pls.President
Angular style guideAccelerometer
T
39

You need to add your components to Dictaat.module.ts's exports in order for them to be imported in your app.module.ts:

//Dictaat.module.ts
import { DictaatComponent } from './dictaat.component';
import { DictaatEntryComponent } from './dictaat-entry.component';

@NgModule({
    imports: [BrowserModule, HttpModule],
    declarations: [DictaatComponent, DictaatEntryComponent],
    exports: [DictaatComponent, DictaatEntryComponent]
})

export class DictaatModule{ }
Tomasine answered 19/9, 2016 at 11:44 Comment(1)
Thanks for the Quick reply! The issue is now fixed. Can't mark as a correct answer yet. Will do it in a few minutes.Inconvenient
E
9

The components, directives, and pipes that should become available by importing this module, need to be listed in exports. declarations is to make these components available within the module:

@NgModule({
    imports: [BrowserModule, HttpModule],
    declarations: [DictaatComponent, DictaatEntryComponent],
    exports: [DictaatComponent, DictaatEntryComponent],
})
export class DictaatModule{ }
Eyepiece answered 19/9, 2016 at 11:44 Comment(1)
Same here, thanks for the anser. You're solution was the same as Stefan Svrkota.Inconvenient
I
3

For ionic devs, using lazy loading pages. This "it isn't a known property of" error can occur even if you import it at the App Module level.

The reason for that is you are using lazy loading feature from ionic.

So to fix it you just need to import it at the Page Module level.

Good resources to understand lazy loading.

http://blog.ionic.io/ionic-and-lazy-loading-pt-1/

http://blog.ionic.io/ionic-and-lazy-loading-pt-2/

Import answered 10/8, 2017 at 10:3 Comment(0)
W
1

For me, I have many custom components, so that I created one custom-view.module.ts and exports all components.

Other modules want to use custom views must to import CustomViewModule

custom-view.module.ts

@NgModule({
  imports: [
    CommonModule
  ],
  declarations: [ RatingComponent ],
  exports: [ RatingComponent ]
})
export class CustomViewModule { }

And in other modules which want to use custom view (RatingComponent in this case)

@NgModule({
  imports: [
    CustomViewModule
  ]
})

export class OtherModule { }

I'm using angular 4+, and exporting DictaatComponent and then importing DictaatModule to app module doesn't work to me. I still have to import all DictaatModule to each module which wants to use DictaatComponent.

Whitsunday answered 20/6, 2017 at 7:29 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.