2

我有一个BookDetailComponent为 url 映射的组件/books/:id。angular 2 路由器中是否有任何方法可以确保仅在从服务器检索到Bookwith given后才打开该组件?id

我正在寻找类似的功能,如Angular 2 路由器中的ui-router 解析

/*** BookComponent ***/
@RouteConfig([
  {path: "/books/:id", component: BookDetailComponent, as: "BookDetail"},
])

export class BookComponent {
}


/*** BookDetailComponent ***/
export class BookDetailComponent {

  book:Book;

  constructor(private bookService:BookService,
              private routeParams:RouteParams) {
  }


  ngOnInit() {
    let id = this.routeParams.get("id");
    this.bookService.getBook(parseInt(id))
      .subscribe(book => this.book = book.json());
  }
}
4

1 回答 1

5

是的,通过实现OnActivate接口并返回您等待加载的对象的承诺:

https://angular.io/docs/js/latest/api/router/OnActivate-interface.html

于 2016-01-12T02:01:49.227 回答