[Angular] Standalone component - routes top level provide share for all child routes

Provide application level module in bootstrapApplication

bootstrapApplication(AppComp, {
  providers: [
    importProvidersFrom(HttpClinetModule)
  ]
})

//BAD
@Component({
 standlone: true,
 imports: [CommonModule, RouterModule, /*bad*/ HttpClinetModule]
})
class OldCmp {}

@NgModule({
  declaractions: [OldCmp],
  imports: [CommonModule, HttpClinetModule, RouterModule]
})

 

When you want to share some provider for app the children component, for example, NgrxStore:

export default const ROUTES = [
  // make an empty parent route
    {
        path: '', 
        providers: [
           importProvidersFrom(NgrxStore.forFeature(...))     
        ],
                               
        children: [
           {path: '', component: HomeCmp}, {path: 'admin', component: AdminCmp, providers: [{provide: AuthService, useClass: AdminAuthService}]}
        ]
    }
]

 

posted @   Zhentiw  阅读(26)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· AI技术革命,工作效率10个最佳AI工具
历史上的今天:
2017-10-02 [TS] Implement a singly linked list in TypeScript
2016-10-02 [Angular2 Router] Configure Auxiliary Routes in the Angular 2 Router - What is the Difference Towards a Primary Route?
2016-10-02 [NodeJS] Use Secrets When Deploying Applications with Now
2016-10-02 [Angular2 Router] Redirects and Path Matching - Avoid Common Routing Pitfall
2016-10-02 [Javascript] Safer property access with Lodash's 'get' method
2016-10-02 [CSS] Introduction to CSS Columns
2016-10-02 [Angular 2] Controlling Rx Subscriptions with Async Pipe and BehaviorSubjects
点击右上角即可分享
微信分享提示