Angular 4路由-带skipLocationChange的redirectTo [英] Angular 4 routing - redirectTo with skipLocationChange
问题描述
我有一些路由模块,其主要路径设置为: / canvas
I have some routing module with its main path being set as: /canvas
const canvasRoutes: Routes = [
{
path: "canvas", component: CanvasComponent
}
];
@NgModule({
imports: [
RouterModule.forChild(canvasRoutes)
],
exports: [
RouterModule
],
declarations: [],
providers: []
})
export class CanvasRoutingModule {
}
在应用程序路由模块中,我希望将重定向路径设置为每个 / canvas
访问根路径的时间。当前的配置如下:
In the application routing module I would like to have the redirection path set to the /canvas
every time the root path is accessed. Currently the configuration looks as follows:
const appRoutes: Routes = [
{
path: "", redirectTo: "/canvas", pathMatch: "full"
}
];
@NgModule({
imports: [
RouterModule.forRoot(appRoutes)
],
exports: [
RouterModule
],
declarations: [],
providers: []
})
export class AppRoutingModule {
}
它可以正常工作,并且可以访问 http:// localhost:4201
被重定向到 http:// localhost:4201 / canvas
。
It works correctly and access to the http://localhost:4201
is being redirected to the http://localhost:4201/canvas
.
但是,我不想拥有重定向后,将 / canvas
路径附加到URL。如何做到这一点?例如,有一种方法可以将 skipLocationChange
参数应用于此重定向,因为我将其与 router.navigate(... {skipLocationChange:true})
?
However, I do not want to have the /canvas
path appended to the url after redirection. How can this be achieved? Is there for example a way, that I could apply the skipLocationChange
parameter to this redirection as I am using it with the router.navigate(... {skipLocationChange: true})
?
推荐答案
我通过订阅<$ AppComponent
中的c $ c> router.events 并手动导航到 canvas
路径将 skipLocationChange
设置为true。
I have resolved that problem by subscribing to the router.events
in the AppComponent
and manually navigating to the canvas
path with skipLocationChange
set to true.
@Component({
...
})
export class AppComponent {
constructor(private router: Router) {
this.router.events.subscribe(routerEvent => {
if (routerEvent instanceof NavigationStart) {
if (routerEvent.url == "/") {
this.router.navigate(["canvas"], {skipLocationChange: true})
}
}
});
}
}
这篇关于Angular 4路由-带skipLocationChange的redirectTo的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!