簡體   English   中英

Ember.js 2.嵌套路由,但非嵌套模板

[英]Ember.js 2. Nested routes but non-nested templates

首先,感謝大家在本論壇中所做的努力。

我有這種情況:

  • mysite.com/authors(作者列表)
  • mysite.com/author/1(作者1)
  • mysite.com/book/1(作者1的書1)
  • mysite.com/cart/1(作者1的書1的購物車頁面)

我需要這樣的網址:

mysite.com/author/1/1/cart

但我不希望使用嵌套模板(每個頁面都不同,並且我不想在多個頁面中使用相同的信息)。

有沒有辦法獲得該URL?

我實際的router.js是這樣的:

Router.map(function() {
  this.route('index', {path: '/'});
  this.route('login');
  this.route('authors', {path: '/authors'});
  this.route('author', {path: '/author/:author_id'});
  this.route('book', {path: '/book/:book_id'});
  this.route('cart', {path: '/cart/:cart_id'});
});

您有兩種選擇:

多個動態細分

只是不要嵌套您的路線,而是有多個動態細分。 那是可能的:

this.route('cart', {path: '/cart/:author_id/:cart_id'});

這將為您提供如下路線:

/cart/1/A這將顯示作者1和購物車A的cart路線

築巢,但不要在父路線中放任何東西

因此,您可以擁有此路由器:

this.route('author', {path: '/author/:author_id'}, function() {
  this.route('cart', {path: '/cart/:cart_id'});
});

現在的問題是,如果將作者數據放入/author路由中,那么它也可以在author.cart路由上author.cart 但是一種簡單的解決方案是僅以{{outlet}}為模板,將author路由保留為空,然后將您的作者內容放入author.index路由中。

這將為您提供如下路線:

/author/1這將與author 1一起呈現author.index路由

/author/1/cart/A這將使用作者1和購物車A呈現author.cart路線

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM