Angular 6 Services: providedIn: 'root' vs CoreModule

后端 未结 7 966
猫巷女王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:34

    I think creating a core module became obsolete since a service can define itself to be registered to root via providedIn: root

    The (old!) angular style guide recommended to create a core module till v6: https://v6.angular.io/guide/styleguide

    But since v7 there is no recommendation anymore to create a core module. I think it is exactly for that reason (providedIn: root does the job).

提交回复
热议问题