Meteor JS Iron Router,路由子目录

时间:2015-10-12 06:22:26

标签: javascript meteor iron-router

我正在使用Iron:Router。在Meteor JS中开发管理员和客户端门户。

我知道我可以使用以下方式创建路线:

this.route('tasks',{path:'/projects', layoutTemplate: 'adminLayout'});

但是是否可以使用子目录创建路径,例如:

this.route('tasks',{path:'/admin/projects', layoutTemplate: 'adminLayout'});

这样我也可以有一个子目录:

this.route('/admin/projects', {name: 'admin.projects', template: 'projects', layoutTemplate: 'adminLayout'}

this.route('/client/projects', {name: 'client.projects', template: 'projects', layoutTemplate: 'adminLayout'}

感谢您的任何意见。

1 个答案:

答案 0 :(得分:2)

您拥有的所有路径都可以非常愉快地在一个应用中共存。 路由器(或您的浏览器)没有任何目录/子目录的概念,它只能理解字符串和正则表达式。嵌套纯粹是我们(应该)创建的东西,使我们能够理解app / api(/ codebase等)的结构。

正如Sasikanth所指出的那样,这不是完整的错误信息。然而,看packages/iron_middleware-stack/lib/middleware_stack.js line 31,很容易确认发生了什么:

    throw new Error("Handler with name '" + name + "' already exists.");

这是在Router.route函数中,记录为here

Router.route('/post/:_id', {
  // The name of the route.
  // Used to reference the route in path helpers and to find a default template
  // for the route if none is provided in the "template" option. If no name is
  // provided, the router guesses a name based on the path '/post/:_id'
  name: 'post.show',

  // To support legacy versions of Iron.Router you can provide an explicit path
  // as an option, in case the first parameter is actually a route name.
  // However, it is recommended to provide the path as the first parameter of the
  // route function.
  path: '/post/:_id',

  // If we want to provide a specific RouteController instead of an anonymous
  // one we can do that here. See the Route Controller section for more info.
  controller: 'CustomController',

  // If the template name is different from the route name you can specify it
  // explicitly here.
  template: 'Post',

  // and more options follow

因此,对于上面包含的代码,您提供了显式路径。因此,第一个参数是路径名称。这些必须是唯一的,因为它们用于在pathFor,urlFor和linkTo帮助器中查找路径。由于您没有提供显式模板选项,因此该名称也用于此,但您的代码在它到达那么远之前就抛出了这个异常。

我认为你想要实现的是:

this.route('/projects', {name: 'projects', template: 'tasks',  layoutTemplate: 'adminLayout'});
this.route('/admin/projects', {name: 'admin.projects', template: 'tasks', layoutTemplate: 'adminLayout'});
this.route('/client/projects', {name: 'client.projects', template: 'tasks', layoutTemplate: 'adminLayout'});