zl程序教程

您现在的位置是:首页 >  前端

当前栏目

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

Angular for all Component Top level child share
2023-09-14 09:00:45 时间

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}]}
        ]
    }
]