簡體   English   中英

為什么在ember js中的任何路由下都需要索引路由?

[英]Why we need a index route under any route in ember js?

我最近正在學習ember js。 但是我不明白的一件事是,為什么對於所有常規路由,總是有一個默認的子路由“索引”。 我們為什么需要它? 有沒有用例?

通過使用index路由,您可以顯示子路由不可見的內容。 例如以下路由器:

Router.map(function () {
  this.route('parent', function () {
    this.route('child');
  });
});

父模板:

<p>I am the parent<br>
This template is visible if a user visits both /parent and /parent/child routes</p>

{{outlet}}

parent.index模板

<p>I am still the parent<br>
This template is only visible if a user visits the /parent route<</p>

parent.child模板

<p>I am the child<br>
This template is only visible if a user visits the /parent/child route</p>

注意: parent.index模板和子模板都在{{outlet}}呈現!

索引路徑是您輸入路徑時要輸入的主要路徑。 例如,您輸入/ blogs,加載的模板是blogs.index模板。

為了更好地理解,您可以檢查http://alexspeller.com/ember-diagonal/route/post並通過單擊特定節點來玩樹視圖,然后可以看到

  • 網址
  • 路線
  • 調節器
  • 模板模板大綱(此處將顯示模板的層次結構)
  • 路由掛鈎(驗證階段掛鈎[ beforeModel()model()afterModel()和設置階段掛鈎[ activate()setupController()renderTemplate() ))。

我只是想解釋一下,

默認情況下,如果您尚未定義任何路由,

Router.map(function() {
});

默認情況下,您將擁有applicationindex路由。 始終記住, 父路由沒有URL ,您將始終處於該路由的子狀態(例如application.index)。

如果您有子路線,那么您肯定在父模板中有{{outlet}} ,這樣子模板才會顯示在父模板出口處。

在這里,您不能單獨訪問應用程序路由,如果您說URL為/則表示application.index路由,因此它將運行application路由鈎子和index鈎子。 它會呈現application.hbs和將呈現index.hbs{{outlet}}所述的application.hbs

暫無
暫無

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

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