首页 > 解决方案 > 将带有子级的速记 URL 重定向到普通 URL

问题描述

我正在尝试实现重定向到常规 URL 的速记 URL,以提高它们在地址栏中键入的效率。我正在尝试为该crates.io项目实现这一点。

为了澄清我的意思,我想像这样重定向 URL:


以下是当前配置的相关长路线的片段:

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文档,但没有找到解决方案。我觉得一定有比我尝试过的更好的方法。谁能指出我正确的方向?

标签: javascriptember.jsroutesember-router

解决方案


推荐阅读