为什么我们在ember js中的任何路由下都需要一个索引路由?

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

最近在学习emberjs。但是我不明白的一件事是,为什么对于所有常规路由,总是有一个默认子路由 "index" 。为什么我们需要它?有什么用例吗?

索引路线是每次进入路线时进入的主路线。例如,您输入 /blogs,加载的模板是 blogs.index 模板。

通过使用 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}}!

中呈现

为了更好地理解,您可以检查 http://alexspeller.com/ember-diagonal/route/post 并通过单击特定节点来使用树视图,您可以看到

  • URL
  • 路线
  • 控制器
  • 模板模板大纲(这将显示 模板层次结构)
  • 路由挂钩(验证阶段挂钩[beforeModel()model()afterModel()] 和 设置阶段挂钩[activate(),setupController(),renderTemplate()]).

我只是想解释一下,

默认情况下,如果您没有定义任何路由,

Router.map(function() {
});

默认情况下,您将拥有 applicationindex 路线。永远记住父路由没有URL,你将永远处于这条路由的子状态(例如application.index)。

如果你有子路由那么你肯定应该在父模板中有 {{outlet}} 这样子模板就会显示在父模板出口中。

这里不能单独访问应用路由,如果说URL是/那么就是application.index路由,所以会运行application 路由挂钩和 index 挂钩。它将呈现 application.hbs 并在 application.hbs.

{{outlet}} 中呈现 index.hbs