meteor Tracker.autorun 获取当前路线(无法读取 null 的 属性 'route')

meteor Tracker.autorun to get current route(Cannot read property 'route' of null)

我有以下代码:

    Tracker.autorun(function () {
      var currentRouter = Router.current().route.getName()
      console.log('Current route is:',currentRouter);
    });

我在控制台中收到以下错误。

Cannot read property 'route' of null

每次路由器更改路径时,我都试图获取当前路由名称。

看看hooks

您可以为此使用 Router.onBeforeAction(function () {。 它会在路由器上的每个动作之前回调。