javascript - 将带有子级的速记 URL 重定向到普通 URL
问题描述
我正在尝试实现重定向到常规 URL 的速记 URL,以提高它们在地址栏中键入的效率。我正在尝试为该crates.io
项目实现这一点。
为了澄清我的意思,我想像这样重定向 URL:
/c
=>/crates
/c/rand
=>/crates/rand
/c/rand/0.5.5
=>/crates/rand/0.5.5
/c/...
=>/crates/...
以下是当前配置的相关长路线的片段:
this.route('crates');
this.route('crate', { path: '/crates/:crate_id' }, function() {
this.route('download');
this.route('versions');
this.route('version', { path: '/:version_num' });
this.route('reverse-dependencies', { path: 'reverse_dependencies' });
this.route('owners');
this.route('docs');
this.route('repo');
});
这是我尝试过的;我确实尝试通过定义以下附加路由来实现这些重定向:
this.route('c');
this.route('c', { path: '/c/:crate_id' });
有了这个./routes/c.js
:
import Route from '@ember/routing/route';
export default Route.extend({
afterModel(crate, path) {
if (crate === undefined) this.transitionTo('crates');
else this.transitionTo('crate', crate, path);
},
});
这仅部分起作用。和路线被覆盖/c
并且/c/rand
可以工作,而其他路线则/c/rand/0.5.5
不起作用。
我可以实现所有子路径的可能性,但我认为这不是正确的做法。我想防止重复代码。
我将如何正确实施?是否可以将任何从/c
它们各自的长路线开始的东西重定向?我查看了ember
文档,但没有找到解决方案。我觉得一定有比我尝试过的更好的方法。谁能指出我正确的方向?
解决方案
推荐阅读
- java - 如何使用 json 文件作为正文执行发布请求
- python - 比较 Pandas Python 中的两个表
- android - 格式化电子邮件以获得最佳通知外观
- java - 用零替换随机数组数
- excel - excel - 需要一个 vba:如果 len(worksheet.tab.name) <3 然后将颜色更改为 vbgreen 结束。谢谢
- c++ - rpath 未在复杂的 make 命令中设置(出现两次,首先优先?)
- javascript - 如何触发 VueJ
- node.js - socket.on('data') 中的数据缓冲区是否可以安全保留?
- html - 如何将伪元素与底部对齐
- javascript - 尝试发送 html - php 网站表单时未捕获的语法错误