angular - Angular: Difference in runtime with Providers in AppModule and providedIn: 'root'?
问题描述
We are currently searching a way to use decorators in our REST services to simplify the process, and I've found a solution after regrouping a lot of research. However, there is still a small issue.
With a @API()
class decorator using mixins, we are able to inject services and extend our REST Service to get the same base injected services, to set the baseAPI and so on...
Use Case 1
@Injectable()
@API({ useExternalSources: true })
export class VehicleService extends APIProperties { }
@NgModule({
...
providers: [
VehicleService
],
bootstrap: [AppComponent]
})
export class AppModule { }
Use Case 2: However, when it comes to use these services without specifying the providers
but using @Injectable({ providedIn: 'root' })
, it fails to recognize the injected services and app breaks.
@Injectable({ providedIn: 'root' })
@API({ useExternalSources: true })
export class VehicleService extends APIProperties { }
@NgModule({
...
providers: [],
bootstrap: [AppComponent]
})
export class AppModule { }
Question: Why app breaks when using providedIn: 'root'
instead of specifying services in AppModule providers
?
Here is a link to the project to Github, so you can clone it and reproduce. Master branch uses Use Case 1, and the branch with-provided-in
uses Use Case 2
Moreover, even if it is working, we still have a warning since Angular v12:
DEPRECATED: DI is instantiating a token "" that inherits its @Injectable decorator but does not provide one itself. This will become an error in a future version of Angular. Please add @Injectable() to the "" class.
-> In a class decorator, it seems quite difficult to add this @Injectable
Thank you in advance and have a nice day!
解决方案
推荐阅读
- postgresql - ST_3DClosePoint 返回多个点
- sql - StoredProcedure 性能改进
- u-boot - 使用 ELDK 交叉编译器在 fedora 8 上编译 U-boot-1.1.6 时出错
- android - 如何解决 gradle 错误“此项目不使用 Gradle 构建系统”
- sql - SQL Query ORDER BY on column not on select on join tables
- clearcase - ClearCase 使用 CAL 或 clearTool 创建 Web 视图
- javascript - 将 Javascript 从 Codepen 转移到 main.js
- java - Spring集成路由到不同类中的子流
- neo4j - 计算每个 Trip 的关系数
- python - 打印所有能被 7 整除并包含 7 从 0 到 100 的数字