在Angular2中,是否可能在主组件之前解析服务?
{
path: '',
resolve: AppComponentResolve,
children: [
{
path: 'somePath',
component: SomeCOmponent
}]
}
@Injectable()
export class AppComponentResolve implements Resolve<any> {
resolve(){
//return resolved data
}
}
在应用程序的默认解析中执行服务
export const appRoutes: Routes = [
{path:'', pathMatch: 'full', redirectTo: 'home'},
{
path: 'home',
component: AppComponent,
resolve: function(){
//your service call
},
{
path: 'path1/:id',
component: YourComponent,
resolve: {
someObject: ComponentResolve
}
}
];
@Injectable()
export class ComponentResolve implements Resolve<Interface> {
constructor(private yourService: YourService) {}
resolve(route: ActivatedRouteSnapshot) {
return this.yourService.getMethod(route.params['id']);
}
path: ''
重定向到path: 'home',component: 'HomeComponent'
,AppComponent 由@NgModule({.....bootstrap:[AppComponent]})
引导。 - undefinedplatformBrowserDynamic().bootstrapModule(AppModule);
的main.ts文件。 - undefined{path:'', pathMatch: 'full', redirectTo: 'home', resolve:{...}},{path: 'home', component: HomeComponent}
并没有起作用。 - undefinedAppComponent
,但这并不意味着它是被引导的组件。 - undefined