ember 2.0嵌套动态模板呈现错误的模板

时间:2015-10-08 09:01:16

标签: ember.js ember-router

其他人在stackoverflow上发布了类似的问题,但还没有答案......

我的第一条动态路线有效。我在第一个动态路由中有一个嵌套的动态路由,但是从不显示该嵌套路由的模板。

此路线有效(显示post.hbs):

#/home/post/29

但对于这条路线:

#/home/post/29/comment/123

我希望显示comment.hbs,但仍会显示post.hbs。

我的路由器看起来像:

this.route('home', { path: '/home'}, function() {
    this.route('post', {path: "/post/:post_id"}, function() {
        this.route('comment', {path: "/comment/:comment_id"}, function() {
        ....

在我的post.hbs中,我有这个:

<div {{action 'doNavigateToComment' comment}}>

在我的路线\ home \ post.js中我有:

doNavigateToComment(comment_id) {
  this.transitionTo('home.post.comment', this.get('postId'), comment_id);
}

我的文件结构如下所示:

\routes
  home.js
  \home
    post.js
    \post
      comment.js
\templates
  \home
    index.hbs
    post.hbs
    post\
      comment.hbs

在控制台中打开了跟踪功能,但没有显示错误。

我正在使用ember 1.13.5(其行为类似于ember 2.0)

1 个答案:

答案 0 :(得分:3)

{{outlet}}中需要post.hbs才能在comment.hbs内进行渲染。

编辑: 由于您要求post替换为comment,您可以使用以下选项之一: 1)修改您的路由器,使comment无法嵌套到post,但嵌套到home

this.route('home', { path: '/home'}, function() {
    this.route('post', {path: "/post/:post_id"}, function() {});
    this.route('comment', {path: "/comment/:comment_id"}, function() {});
    ....

2)将post.hbs更改为您想要的行为:

{{#if pathIsForPosts}}
    ...
{{else}}
    {{outlet}}
{{/if}}

pathIsForPosts可以是计算属性,用于分析path并返回truefalse,如果它是帖子或评论的路径。

相关问题