Meteor JS Iron Router, route sub directory

怎甘沉沦 提交于 2019-12-12 03:17:41

问题


I am working on a admin and client portal in Meteor JS using Iron:Router.

I know i can create a route using:

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

But is it possible to make a route with a sub directory such as:

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

So that way i can also have a sub directory of:

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

and

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

Thanks for any input.


回答1:


All the paths you have can coexist in one app quite happily. The router (or your browser) doesn't have any concept of directories/subdirectories, all it understands are strings and regular expressions. The nesting is purely something we (should) create to enable ourselves to understand how an app/api(/codebase, etc) is structured.

As Sasikanth points out that is not the full error message. However looking at packages/iron_middleware-stack/lib/middleware_stack.js line 31 it's easy to confirm what is happening:

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

This is within the Router.route function, which is documented 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

So for the code you included above, you provide explicit paths. Therefore the first parameter is the route name. These must be unique as they are used to lookup the path in the pathFor, urlFor and linkTo helpers. As you are not providing an explicit template option, the name is also used for that, but your code is throwing this exception before it gets that far.

I think what you were trying to achieve is this:

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'});


来源:https://stackoverflow.com/questions/33074451/meteor-js-iron-router-route-sub-directory

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!