查看子路线时,灰烬隐藏父模板

rob*_*bby 4 javascript handlebars.js ember.js

我正在用Ember为我的网站制作一个简单的博客。

我的路线:

Router.map(function() {
  this.route('home', { path: '/' });
  this.route('blog', function() {
    this.route('post', {path: '/:post_id'});
  });
});
Run Code Online (Sandbox Code Playgroud)

我想要它,所以当我单击一个帖子/blog并结束时,/blog/:post_id我隐藏了blog.hbs文件的内容,只显示blog/post.hbs内容。

我尝试在post.js路由文件中显式指定渲染模板,但是事情仍然以相同的方式进行。

export default Ember.Route.extend({
  model: function(params) {
    return this.store.findRecord('post', params.post_id, { reload: true });
  },
  renderTemplate: function() {
    this.render('blog/post');
  }
});
Run Code Online (Sandbox Code Playgroud)

阅读这里的文档并没有提示我任何其他想法。

https://guides.emberjs.com/v1.13.0/routing/rendering-a-template/

我从根本上使用Ember会出错吗?如果我想隐藏父模板的内容,我的帖子URL应该不是Blog的子路由吗?

nge*_*elx 5

抱歉,@ remi回答可以完成工作,但是按照惯例这是不正确的。

创建类似这样的内容时:

Router.map(function() {
  this.route('blog', function() {
    this.route('post', {path: '/:post_id'});
  });
});
Run Code Online (Sandbox Code Playgroud)

它隐式创建

Router.map(function() {
  this.route('blog', function() {
    this.route('index', { path: '/' }); // <------ this one
    this.route('post', {path: '/:post_id'});
  });
});
Run Code Online (Sandbox Code Playgroud)

因此,如果您嵌套了路由器,但是不想嵌套模板,则将博客模板(和路由)重命名为索引。就像重命名一样:

带POD

app / blog / route.js-> app / blog / index / route.js

app / blog / template.hbs-> app / blog / index / template.hbs

没有POD

app / routes / blog.js-> app / routes / blog / index.js

app / templates / blog.hbs-> app / templates / blog / index.hbs

官方指南中对此进行了记录(该链接适用于最新版本,但也适用于较旧的版本)