When we import BrowserModule in root module of application ,we can use NgIf and NgFor( in eagerly loaded component). But for lazy loaded modules I have to import CommonModule which has been exported by BrowserModule of root. SO why do we have to import it again in lazy loaded module?
相关问题
- Angular RxJS mergeMap types
- npm WARN optional SKIPPING OPTIONAL DEPENDENCY: fs
- How to update placeholder text in ng2-smart-table?
- How to instantiate Http service in main.ts manuall
- Angular: ngc or tsc?
相关文章
- angular脚手架在ie9+下兼容问题
- angular 前端项目 build 报错 "Cannot find module 'le
- Angular Material Stepper causes mat-formfield to v
- After upgrade to Angular 9 cannot find variable in
- is there any difference between import { Observabl
- Suppress “Circular dependency detected” suppress w
- How can you get current positional information abo
- Angular material table not showing data
As Ward Bell said(https://devchat.tv/adv-in-angular/119-aia-avoiding-common-pitfalls-in-angular2):
Modules don't inherit access to the components, directives, or pipes that are declared in other modules. (https://angular.io/guide/ngmodule#add-the-contactmodule see orange block)
That's why you have to import
CommonModule
to have access tongIf
,ngFor
and so on directives. Your module doesn't know anything about directives from other modules. It only looks at itsdeclarations
andexports
from imported modulesSee also:
The Browser module is used in the root module of the app and when we are using modules the imports are specific to the modules. When using feature modules for lazy loading components we have to import common module in the feature module, so as to tell Angular that the browser modules whixh contains some build in directives like ngFor and ngIf to be available in the feature module too.
Remember that the import for browser module will be only used only once in the app.module and where ever else we want to use these built in directives in our feature or shared modules we need to import the common module.