gpt4 book ai didi

javascript - 查找 Iron Router 中定义的所有路由

转载 作者:行者123 更新时间:2023-11-30 16:36:34 25 4
gpt4 key购买 nike

有没有办法为 Iron Router Router 中定义的所有路径动态添加站点地图?

我在想这样的事情:

<ul>
{{#each paths}}
<li><a href="pathFor {{route}}">{{route}}</a></li>
{{/each}}
</ul>

此外,也许相应的子路径将显示在子 ul 中?

显然,我也可以手动创建列表,但对于具有 50 多个链接的应用来说,这是一项艰巨的工作。

最佳答案

当然!正如 Kassym 指出的那样,Router.routes包含所有路由的列表。但是它包含路由器功能,因此您必须使用 route.<b>getName()</b> 获取它们的名称。 .默认路由没有名称,因此您必须获取 .path()相反。

整个事情在你的助手中应该是这样的:

Template.allRoutes.helpers({
paths: function () {
var allRoutes = _.map(Router.routes, function(route){
var routeName = typeof route.getName() === 'undefined' ?
route.path() :
route.getName();
return {route: routeName}
});
return allRoutes
}
});

这在你的模板中:

<template name="allRoutes">
{{#each paths}}
<li><a href="{{pathFor route}}">{{route}}</a></li>
{{/each}}
</template>

Working Demo in MeteorPad

Note: Remember to enclose pathFor in curly brackets because it is a helper method. It will execute javascript and inherit the current datacontext, so you can pass it any property from the current context.

为了显示 n 深度的子路径,您可以像这样递归调用您的模板:

<template name="subpaths">
<ul>
{{#each subpaths}}
<li>
<a href="{{pathFor path}}">{{path}}</a>
{{#if subpaths}} {{>subpaths}} {{/if}}
</li>
{{/each}}
</ul>
</template>

Demo in meteor pad

有关详细信息,请参阅 getting the names of all routes for the accounts-entry package

关于javascript - 查找 Iron Router 中定义的所有路由,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32629120/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com