【问题标题】:Meteor JS Iron Router, route sub directoryMeteor JS Iron Router,路由子目录
【发布时间】:2015-10-13 01:28:58
【问题描述】:

我正在使用 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'}

感谢您的任何意见。

【问题讨论】:

  • 这是完整的错误报告吗?看起来只有一半的错误,如果没有,您可以发布完整的错误或错误的屏幕截图。
  • 也尝试更改路线的名称,对于您使用相同名称的所有路线 tasks ,我认为路线名称应该不同
  • 嗨,我已经让错误消失了,错误日志实际上来自重复的路由(管理员/客户端),这就是我想区分它们的原因。但是现在的问题是该路由无法解决。 Iron:Router 可以识别路由,但不知道要加载什么模板,从而使页面处于加载状态,只加载了 CSS。
  • 而他们都阅读模板任务的原因只是因为我在这里复制并粘贴了它们。他们都将使用不同的模板。仅供参考。

标签: javascript meteor iron-router


【解决方案1】:

您拥有的所有路径都可以非常愉快地共存于一个应用程序中。 路由器(或您的浏览器)没有任何目录/子目录的概念,它所理解的只是字符串和正则表达式。嵌套纯粹是我们(应该)创建的东西,以使我们自己了解应用程序/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'});

【讨论】:

  • 您好,请完全放弃控制台错误,该错误来自重复的路线,正如我在前面的 cmets 中提到的那样,没有发布。您对我已经纠正的尝试实现的目标是正确的,但是在加载 CSS 后,应用程序陷入了加载阶段。我已经将代码更改为:this.route('/admin/projects', {name: 'admin.projects', template: 'projects', layoutTemplate: 'adminLayout'}
  • 您可能应该配置路由器以使用带有Router.configure({ layoutTemplate: 'adminLayout' }); 的布局另外,为了简单起见,现在删除name 属性。考虑颠倒您的命名约定,即它是/projects/admin/projects/client。至于您遇到的实际问题,请尝试确保您的所有项目文件中只有一个名为tasks 的模板,有时重复会潜入并导致奇怪的错误。如果这没有帮助,我们可能需要您将一些错误粘贴到控制台或流星服务器中。
  • 也刚刚注意到,但是您正在使用 this.route 而不是 Router.route - 我认为它已被弃用了几个版本 - 请参阅Iron-Router 的官方入门指南
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-03-20
  • 1970-01-01
  • 1970-01-01
  • 2017-03-05
  • 2015-11-30
相关资源
最近更新 更多