CommonModule vs BrowserModule in angular

前端 未结 4 957
鱼传尺愫
鱼传尺愫 2020-12-15 05:04

I am pretty new to the world of Angular. What is the difference between CommonModule vs BrowserModule and why one should be preferred over the othe

4条回答
  •  隐瞒了意图╮
    2020-12-15 05:25

    So, based on the definitions of 'BrowserModule' and 'CommonModule', my suggestion for using them is something like this, You should have the 'BrowserModule' in application module level, 'AppModule':

    app.module.ts: 
        
             import { BrowserModule } from '@angular/platform-browser';
             imports: [
                    BrowserModule.withServerTransition({ appId: 'ng-cli-universal' })]
    

    having the 'CommonModule' in custom component level, in a SharedModule among the other common modules that you use in the project:

    shared.module.ts: 
     import { CommonModule } from '@angular/common';
     import { FormsModule } from '@angular/forms';
     import { RouterModule } from '@angular/router';
        
     imports: [
            CommonModule,
            FormsModule,
            RouterModule     
          ], 
        exports: [
            CommonModule,
            FormsModule,
            RouterModule]
    

    Then inject this 'SharedModule' into any other custom modules, like this:

    login.module.ts:  
    
         import { SharedModule } from '../../shared/shared.module';
            imports: [
                SharedModule]
    

提交回复
热议问题