流星铁路由器从模板助手中的参数获取当前路径

Ric*_*old 14 meteor iron-router

在模板助手中,我从Iron.Router(iron:router)获取当前路径,如下所示:

Router.current().route.path()
Run Code Online (Sandbox Code Playgroud)

这工作正常,除非路径路径确实包含参数(例如/client/:_id/edit).在那种情况下,path()函数返回null.

当路由包含参数时,如何获取模板助手中的当前路径?

我正在使用Meteor 1.0和iron:router1.0.1

sai*_*unt 14

我想_id你的路线来自一个集合,你需要传递route.path路线所依据的文件.

Router.route("/client/:_id/edit",{
  name:"edit",
  data:function(){
    return MyCollection.findOne(this.params._id);
  }
});

<template name="edit">
  {{myHelper}}
  {{pathFor route="edit"}}
</template>

Template.edit.helpers({
  myHelper:function(){
    return Router.current().route.path(this);
  }
});
Run Code Online (Sandbox Code Playgroud)

我建议您使用默认pathFor帮助程序在应用程序中呈现URL.

https://github.com/EventedMind/iron-router/blob/devel/Guide.md#pathfor

该帮助程序使用当前数据上下文(在本例中MyCollection.findOne(this.params._id))来提取路径参数.

但是您也可以使用路径中的路径方法,该方法将要生成路径的文档作为第一个参数.


Sam*_*uel 8

尝试以下替代方案:

Iron.Location.get().path;
Run Code Online (Sandbox Code Playgroud)

当我需要带参数的路径时,对我来说很好.但是,不会返回当前路线.

请参阅流星铁路由器未从路径获取当前路径