这.route()和这.resource()有什么区别?(涉及IT技术)

6

我理解路由是用于将URL映射到模板的,但显然你可以使用this.route或者this.resource来定义路由。

App.Router.map(function() {
  this.route("about", { path: "/about" });
  this.route("favorites", { path: "/favs" });
});

App.Router.map(function() {
  this.resource('posts', { path: '/posts' }, function() {
    this.route('new');
  });
});

如果您想定义子路由到一个路由,您是否只需使用this.resource,还是有其他原理我没有理解的?

2个回答

8
你只需要使用this.resource来定义路由的子路由,这是基本思想。resource代表父级(通常是名词),而route代表子级(通常是动词)。同时请注意,路由器总是指向当前路由,它无法转换到资源。在幕后,Ember会自动生成每个资源下面的“index”路由。因此,即使你定义了如下内容:
App.Router.map(function() {
  this.resource("about", { path: "/about" });
});

你最终得到的结果是这样的:
App.Router.map(function() {
  this.resource('about', { path: '/about' }, function() {
    this.route('index');
  });
});

谢谢! :) 但是你不能在资源下定义另一个资源,对吧?如果你想让你的URL看起来像/contacts/<contact_id>/edit(就像这里)link怎么办? - Marco Petersen
2
你应该能够这样做 - "联系人" 将成为根下的一个资源(例如由 ArrayController 支持),“:contact_id” 则是另一个资源(由 ObjectController 支持),并且在其中有一个“edit”路由。请查阅文档中的嵌套资源部分:http://emberjs.com/guides/routing/defining-your-routes/#toc_nested-resources - Joaquim Rendeiro

0

在 Ember 3.x 中,这已经被弃用了。不再有 this.resource()


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接