本文介绍了流星JS Iron Router,路由子目录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时删除!!

我正在使用Iron:Router在Meteor JS中管理和客户端门户.

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

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

感谢您的任何输入.

推荐答案

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

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.

正如Sasikanth指出的那样,这不是完整的错误消息.但是请查看 packages/iron_middleware-stack/lib/middleware_stack.js第31行,很容易确认发生了什么:

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.");

这是在Router.route函数中,该函数记录在此处.

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

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

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

这篇关于流星JS Iron Router,路由子目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

1403页,肝出来的..

09-06 11:44