Angular 6 Services: providedIn: 'root' vs CoreModule

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

    provideIn: root this could be useful in those scenarios where you have multi-application projects. In such kind of scenarios, you have an angular workspace with multiple projects inside it.

    And, you would never want your project to have some kind of dependencies on the workspace's core module as it won't allow you to run your project independently.

    In such a case, you keep your services in the library and use providedIn: 'root' for making them singleton.

提交回复
热议问题