javascript 如何使用 Meteor 和 Iron Router 通过模板中的路由名称检查路由
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22213122/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to check for Route through route name in template with Meteor and Iron Router
提问by Maaz
What can I use in a template to figure out the route name
that is associated with the route that I am currently on?
我可以在模板中使用什么来确定route name
与我当前所在的路线相关联的路线?
For example if I configured a route like so in iron-router
例如,如果我在 iron-router
this.route('quick', {
path: '/wow/:_id',
template: 'create_question'
});
So if I am on the route /wow/123
how can I get the router's name in my template, in this case how can I get quick
in my template?
因此,如果我在路线上,/wow/123
如何在我的模板中获得路由器的名称,在这种情况下我如何获得quick
我的模板?
I'm simply looking for a function, I am sure I can use a handlebars helper to get the rest done. I just need a function to call.
我只是在寻找一个功能,我相信我可以使用车把助手来完成其余的工作。我只需要一个函数来调用。
回答by David Weldon
iron-router > 1.0
铁路由器 > 1.0
var routeName = Router.current().route.getName();
iron-router < 1.0
铁路由器 < 1.0
var routeName = Router.current().route.name;
回答by J Cole Morrison
For the newer iron router, use:
对于较新的铁路由器,请使用:
var routeName = Router.current().route.getName()
This will output the name of the actual route you defined with this.route()
这将输出您定义的实际路线的名称 this.route()
回答by Cedric
You can define any options you want in your route config :
您可以在路由配置中定义所需的任何选项:
Router.route('/', {
name : 'home',
template : 'home',
title: 'Home'
});
and then access to title with this :
然后使用此访问标题:
Router.current().route.options.title
This will output "Home"
这将输出“主页”
回答by ken
You can try Router.current().template
你可以试试 Router.current().template