Angular 6 Services: providedIn: 'root' vs CoreModule

后端 未结 7 904
猫巷女王i
猫巷女王i 2021-02-01 02:40

With Angular 6, below is the preferred way to create singleton services:

import { Injectable } from \'@angular/core\';

@Injectable({
  providedIn: \'root\',
})
         


        
7条回答
  •  挽巷
    挽巷 (楼主)
    2021-02-01 03:30

    That would be true if the CoreModule only contained services. However, it does include other things such as single use components.

    From Angular Docs:

    Do gather application-wide, single use components in the CoreModule. Import it once (in the AppModule) when the app starts and never import it anywhere else. (e.g. NavComponent and SpinnerComponent).

提交回复
热议问题