官术网_书友最值得收藏!

Resolving data

After the router has run the guards, it will resolve the data. To see how it works, let's tweak our configuration from above:

[
  {
    path: ':folder',
    children: [
      {
        path: '',
        component: ConversationsCmp,
        resolve: {
          conversations: ConversationsResolver
        }
      }
    ]
  }
]

Where ConversationsResolver is defined as follows:

@Injectable()
class ConversationsResolver implements Resolve<any> {
  constructor(private repo: ConversationsRepo, private currentUser: User) {}

  resolve(route: ActivatedRouteSnapshot, state: RouteStateSnapshot):
      Promise<Conversation[]> {
    return this.repo.fetchAll(route.params['folder'], this.currentUser);
  }
}

Finally, we need to register ConversationsResolver when bootstrapping our application:

@NgModule({
  //...
  providers: [ConversationsResolver],
  bootstrap: [MailAppCmp]
})
class MailModule {
}

platformBrowserDynamic().bootstrapModule(MailModule);

Now when navigating to /inbox, the router will create a router state, with an activated route for the conversations component. That route will have the folder parameter set to inbox. Using this parameter with the current user, we can fetch all the inbox conversations for that user.

We can access the resolved data by injecting the activated route object into the conversations component:

@Component({
  template: `
    <conversation *ngFor="let c of conversations | async"></conversation>
  `
})
class ConversationsCmp {
  conversations: Observable<Conversation[]>;
  constructor(route: ActivatedRoute) {
    this.conversations = route.data.pluck('conversations');
  }
}
主站蜘蛛池模板: 庆元县| 博湖县| 长海县| 普兰县| 宽甸| 奇台县| 高邮市| 唐河县| 柳林县| 中牟县| 邮箱| 谷城县| 宁化县| 通海县| 霍城县| 大田县| 喀喇沁旗| 兴安县| 彩票| 锦州市| 安宁市| 乌拉特中旗| 周宁县| 丰宁| 巴塘县| 黄大仙区| 龙门县| 和政县| 栾川县| 连山| 武邑县| 万宁市| 六盘水市| 双峰县| 无极县| 揭西县| 河池市| 扶沟县| 安康市| 灌南县| 金阳县|