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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 22:42:01  来源:igfitidea点击:

How to check for Route through route name in template with Meteor and Iron Router

javascriptmeteoriron-router

提问by Maaz

What can I use in a template to figure out the route namethat 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/123how can I get the router's name in my template, in this case how can I get quickin 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