在Iron Routermeteor中覆盖路由

我尝试了两种方法。 首先是定义一个具有相同模式的新路线,但它给我错误说“path已经存在”。

然后,我也尝试从路由器中获取现有的RouteController并对其进行更改,但是操作并不顺利

具体来说,我想在项目望远镜中覆盖下面的路线。

https://github.com/TelescopeJS/Telescope/blob/master/packages/telescope-posts/lib/routes.js#L159-L162

一种方法是修改现有的路线选项。

 // Running it in Meteor.startup is only necessary because Telescope // defines the route at startup Meteor.startup(function () { // Find a route by name var route = Router.routes.posts_default; // OR find a route by path var route = Router.findFirstRoute('/'); // Override existing route options _.extend(route.options, { data: //... // Other route options... }); }); 

另一种方法是删除路线并重新创build它。

使用通过名称删除路由的function,

 function removeRouteByName (routeName) { var routes = Router.routes; var route = routes[routeName]; if (!route) return false; // Returns false if route is not found // Remove route from Router delete routes[routeName]; delete routes._byPath[route.path()]; var routeIndex = routes.indexOf(route); if (routeIndex >= 0) routes.splice(routeIndex, 1); // Remove route handler from MiddleWareStack delete Router._stack._stack[routeName]; Router._stack.length -= 1; return true; // Returns true when route is removed } 

我们可以通过删除和重新创build路由

 // Running it in Meteor.startup is only necessary because Telescope // defines the route at startup Meteor.startup(function () { removeRouteByName('posts_default'); Router.route('/', { name: 'posts_default', // Use the same name //Add your parameters... }); }); 

尝试在这个存储库中运行望远镜,你应该看到路由/已经改变。

使用onBeforeAction钩子将完成这项工作,虽然不是最好的办法。

 Router.onBeforeAction(function () { if (Router.current().route.getName()==="posts_default") { this.render("posts_list_controller", { data: { terms: { // Telescope specific view data view: 'another_view', limit: 10 } } }); } else { this.next(); } });