[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 @ 2022-10-02 21:35  Zhentiw  阅读(26)  评论(0编辑  收藏  举报