The main rule here is that:
The selectors which are applicable during compilation of a component template are determined by the module that declares that component, and the transitive closure of the exports of that module’s imports.
So, try to export it:
@NgModule({
declarations: [TaskCardComponent],
imports: [MdCardModule],
exports: [TaskCardComponent] // <== export the component you want to use in another module
})
export class TaskModule{}
What should I export?
Export declarable classes that components in other modules should be
able to reference in their templates. These are your public classes.
If you don’t export a class, it stays private, visible only to other
component declared in this module.
The minute you create a new module, lazy or not, any new module and you declare anything into it, that new module has a clean state(as Ward Bell said in https://devchat.tv/adv-in-angular/119-aia-avoiding-common-pitfalls-in-angular2)
Angular creates transitive module for each of @NgModule
s.
This module collects directives that either imported from another module(if transitive module of imported module has exported directives) or declared in current module.
When angular compiles template that belongs to module X
it is used those directives that had been collected in X.transitiveModule.directives.
compiledTemplate = new CompiledTemplate(
false, compMeta.type, compMeta, ngModule, ngModule.transitiveModule.directives);
https://github.com/angular/angular/blob/4.2.x/packages/compiler/src/jit/compiler.ts#L250-L251
This way according to the picture above
-
YComponent
can’t useZComponent
in its template becausedirectives
array ofTransitive module Y
doesn’t containZComponent
becauseYModule
has not importedZModule
whose transitive module containsZComponent
inexportedDirectives
array. -
Within
XComponent
template we can useZComponent
becauseTransitive module X
has directives array that containsZComponent
because
XModule
imports module (YModule
) that exports module (ZModule
) that exports directiveZComponent
-
Within
AppComponent
template we can’t useXComponent
becauseAppModule
importsXModule
butXModule
doesn’t exportsXComponent
.
See also