用于主(自举)组件的Angular2旋转变压器?

时间:2017-03-09 22:28:12

标签: angular resolve

是否可以在angular2?

中的主要组件之前解析服务

2 个答案:

答案 0 :(得分:0)

在主路径文件中,在最顶层,您可以提到一个解析器名称,该名称将在最顶层组件加载之前解析(这是自举组件)

    {
    path: '',
    resolve: AppComponentResolve,
    children: [
        {
            path: 'somePath',
            component: SomeCOmponent
        }]
    }

将解析器作为注入添加到appComponent文件

    @Injectable()
    export class AppComponentResolve implements Resolve<any> {

    resolve(){
      //return resolved data
     }
    }

答案 1 :(得分:-1)

在应用默认

下的解析中执行服务
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']);
  }