为了获得易于理解的链接来分享,我不想只在url中放入._id,同时也放入.name。
Router.map(function () {
this.route('here', {
path: 'here/:_id/:name/',
template: 'here'
})
}) 问题是.name条目可以包含像/这样的特殊字符。
www.example.com/here/1234/name_with special-characters like / (<-this slash) /有没有办法替换iron-router中的斜杠(和其他特殊字符)?
(如果有好的方法来处理这个问题,也许在某些情况下我甚至不再需要id。)
如果我想使用<a href="{{pathFor 'showCourse'}}">
我不能使用wildecardpath: 'here/:_id/*
谢谢
发布于 2013-11-18 05:10:44
它不是特定于Iron Router的,但是JavaScript的本地全局函数encodeURIComponent和decodeURIComponent就是为了这个目的而存在的:
encodeURIComponent("foo/bar"); // returns "foo%2Fbar"
decodeURIComponent("foo%2Fbar"); // returns "foo/bar"我在我的项目中所做的是添加一个名为slug的字段,并编写一个函数,该函数从文档的标题生成一个URL友好的slug,并检查该集合以确保该slug是唯一的(否则它会根据需要附加"-2“或"-3”等)。对于每个文档唯一的slug或类似字段,您可以将其用作惟一的查询参数并放弃_id。
发布于 2015-04-11 05:50:40
根据Geoffrey Booth的回答,您可以使用模板帮助器来完成此任务。
定义一个模板帮助器来编码您的name值(我将其设置为全局的,以便它可以被所有模板重用):
Template.registerHelper('encodeName', function() {
this.name = encodeURIComponent(this.name);
return this;
});然后,在您的模板中,您可以将此函数传递给iron-router的pathFor助手:
<a href="{{pathFor 'showCourse' encodeName}}">这在Meteor 1.1.0.2上对我有效。
https://stackoverflow.com/questions/20033586
复制相似问题