Angular 2 - equivalent to router resolve data for new router

前端 未结 2 1448

I am playing with Angular 2.0\'s new router and I try to use something similar to Angular 1.X ui-router / ng-route resolve mechanism.

I was trying to a

2条回答
  •  谎友^
    谎友^ (楼主)
    2020-12-11 04:10

    You have to move your @RouteConfig into the AppCmp constructor:

    //@RouteConfig([
    //  { path: '/', component: HomeCmp, as: 'Home', data: this.history },
    //  { path: '/about', component: AboutCmp, as: 'About' }
    //])
    export class AppCmp {
      history: string[] = [];
      constructor(public list: PersonalizationList,
                  private router_: Router) {
        list.get('histoy', (response) => {
          this.history = response;
        });
        router_.config([
          { path: '/', component: HomeCmp, as: 'Home', data: this.history },
          { path: '/about', component: AboutCmp, as: 'About' }
        ]);
      }
    }
    

    On the console output I could see:

    RouteData {data: "test sample"}

    Hope it helps!

提交回复
热议问题