首页 > 其他分享 >[Angular] Standalone component - routes top level provide share for all child routes

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

时间:2022-10-02 21:45:02浏览次数:43  
标签:providers provide component level share routes path

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

 

标签:providers,provide,component,level,share,routes,path
From: https://www.cnblogs.com/Answer1215/p/16749535.html

相关文章