【问题标题】:How to check current route in Iron Router Meteor如何在 Iron Router Meteor 中检查当前路线
【发布时间】:2015-08-24 07:10:39
【问题描述】:

我正在设置一个与 Iron Router 一起使用的 body helper 以用作{{route}}

Template.body.helpers({
  route: function(){
    alert(Router.current().route.getName());
  }
});

但是Router.current().route.getName() 返回 undefined 而不是“/thirdPage/”。

【问题讨论】:

  • 将 Router.current().route.getName() 替换为 Router.current().route._path

标签: javascript templates meteor iron-router


【解决方案1】:
Router.current().route.path()

工作正常,直到你有带参数的路线,此时它突然重新调整

null

在我看来,这很不可靠。

Iron.Location.get().path

似乎是目前获取带参数路径的最可靠方法(不是完整的 url,包括 Router.current().url 中的“http://”)

【讨论】:

    【解决方案2】:

    我用:

    Router.current().url
    

    这会获取您网址栏中的任何内容, 仅供参考

    Router.current().params.yourParamName
    

    这会得到你参数的内容

    【讨论】:

      【解决方案3】:

      试试:

      Router.current().route.path()
      

      【讨论】:

      • 第一个返回“secondPage”,第二个返回“/secondPage” - 第二个是我一直在寻找的,谢谢
      • 使用公共方法而不是私有属性:Router.current().route.path().
      猜你喜欢
      • 2014-05-06
      • 1970-01-01
      • 2015-04-06
      • 2015-12-16
      • 2015-03-12
      • 1970-01-01
      • 2014-04-08
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多