Hiding/trimming 原始路线的一部分并重定向到它

Hiding/trimming a part of the original route and redirecting to it

我正在研究 angular 4,我想 trim 我当前的 slug 的一部分并将其解析为与原始 slug 相同的组件,这样用户就看不到原始 slug参数。

我有一个路由 route/:slug,它解析为对象并使用名为 EventDetail.component 的对象渲染组件。

我的问题是,是否有可能 trim 那个鼻涕虫的一部分,以便 我的 route/this-is-the-slug-by-person 变成了 route/this-is-the-slug,它解析成相同的组件:EventDetail.component

这是我解决事件的路线

{
    path: 'route/:slug',
    component : EventDetailComponent,
    resolve: {
        event: resolverService 
    }
}

我想要一条路线

{
    path:'route2/:trimmedSlug,
    component : EventDetailComponent,
    resolve: {
      ...
    }
}

我的解析服务

resolve () {
   return this.service.getEvent(this.route.params['slug']);
}

有什么优雅的方法可以做到这一点吗? 谢谢。

使用Location.replaceState.

constructor(public location: Location) {
  this.activatedRoute.paramMap.subscribe(paramMap => {
    const slug = paramMap.get('slug');
    const trimmed = ...;
    this.location.replaceState(trimmed);
  })
}

确保从 @angular/common;

导入 Location