【问题标题】:ember 2.0 nested dynamic templates renders the wrong templateember 2.0 嵌套动态模板呈现错误的模板
【发布时间】:2015-10-09 22:46:38
【问题描述】:

其他人在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}}>

在我的 route\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)

【问题讨论】:

    标签: ember.js ember-router


    【解决方案1】:

    您需要在post.hbs 中使用{{outlet}} 才能在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(如果它是帖子或 cmets 的路径)。

    【讨论】:

    • 这适用于我的情况吗?我在application.hbs 中有{{outlet}},我的post.hbs 应完全替换为comment.hbs
    • 检查emberjs 的文档,我认为您可以通过将index.hbs 添加到post 路由并在post 中仅包含{{outlet}} 来执行选项2。这样访问home/post/id时会渲染index,访问home/post/id/comment/id时会渲染comment
    • 做到了!谢谢!我现在明白使用index.hbs 和不使用之间有很大的不同。 (我确实使用了选项 2 和 index.hbs,如您的评论中所述)
    猜你喜欢
    • 2015-10-15
    • 1970-01-01
    • 1970-01-01
    • 2016-03-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多