简体   繁体   English

路径为铁路由器流星

[英]pathFor for iron router meteor

I am pretty confused about this issue. 我对这个问题很困惑。 I have template which has two paths as follows: 我有模板有两条路径如下:

Router.route('/companyDataManagement',{
        path:['/companyDataManagement','/companyDataManagement/:_id'],
        name: 'companyDataManagement',
        yieldTemplates:{
            'companyData':{to:'showCompanyData'},
            'companyDetails':{to:'showCompanyDetails'}
        }
});

This works perfectly fine. 这完全没问题。 But how do I use pathFor for this template. 但是如何为此模板使用pathFor <a href="{{pathFor companyDataManagement}}">Click</a> does not work <a href="{{pathFor companyDataManagement}}">Click</a>不起作用

Can you confirm if the companyDataManagement in the link is a name being passed from a helper or if you intend this to be the name of the route called? 您能否确认链接中的companyDataManagement是否是从帮助程序传递的名称,或者您是否打算将其作为所调用路由的名称? if it is the latter it needs to be encapsulated in single quotation marks like below 如果是后者,则需要将其封装在如下的单引号中

<a href="{{pathFor 'companyDataManagement'}}">Click</a>

If you want to then pass the :_id into the pathFor this comes from the data context which the link is in, if the data context does not supply the id you need to declare an object to pass into the template inside a helper: 如果你想将:_id传递给路径,这来自链接所在的数据上下文,如果数据上下文没有提供id,你需要声明一个对象传递给帮助器内的模板:

Template.yourTemplate.helpers({
    myContextHelper: function(){
        return {_id:'XXXXXXXXX'}
    }
});

{{#with myContextHelper}}
    <a href="{{pathFor 'companyDataManagement'}}">Click</a>
{{/with}}

Which should give you /companyDataManagement/XXXXXXXXX 哪个应该给你/ companyDataManagement / XXXXXXXXX

You can also pass in the query, hash and data variables using for example query="q=1" or query=qstring where qstring is an object from a helper or a field in the myContextHelper object. 您还可以使用例如query =“q = 1”或query = qstring传递查询,散列和数据变量,其中qstring是来自helper的对象或myContextHelper对象中的字段。

<a href="{{pathFor 'companyDataManagement' query=qstring }}">Click</a>

Additionally and not strictly to do with the question but is hopefully helpful, it looks from your code like you are just having the :id as an optional route part in your path and that the templates themselves do not require an :_id to be specified, in which case you can just use a ? 此外,并不严格地与问题有关但希望有用,它从你的代码看起来就像你只是:id作为你的路径中的可选路由部分,并且模板本身不需要:_id被指定,在这种情况下你可以使用? to make the part optional: 使部件可选:

path:'/companyDataManagement/:_id?',

You can also use this for your opening argument for the route to eliminate having to specify the path in the function: 您还可以将此用作路径的开始参数,以消除必须在函数中指定路径:

Router.route('/companyDataManagement/:_id?',{

Hope this helps! 希望这可以帮助! Let me know if the above doesn't work happy to help troubleshoot if you can post a bit more of the code surrounding it 如果您可以发布更多围绕它的代码,请告诉我上述内容是否无法帮助解决问题

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM