为了获得易于理解的共享链接,我不想只添加 ._id
在 url 中,但 .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'}}">
我无法使用通配符 path: 'here/:_id/*
谢谢
最佳答案
它并不是 Iron Router 特有的,而是 JavaScript 的 native 全局函数 encodeURIComponent
和 decodeURIComponent
就是为了这个目的而存在的:
encodeURIComponent("foo/bar"); // returns "foo%2Fbar"
decodeURIComponent("foo%2Fbar"); // returns "foo/bar"
我在项目中所做的就是添加一个名为 slug
的字段,并编写一个函数,该函数根据文档标题生成 URL 友好的 slug 并 检查集合以生成确保 slug 是唯一的(否则它会根据需要附加“-2”或“-3”等)。对于每个文档唯一的 slug
或类似字段,您可以将其用作唯一的查询参数并放弃 _id
。
关于meteor - Iron-router:替换特殊字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20033586/