Angular 4 routing – redirectTo with skipLocationChange

前端之家收集整理的这篇文章主要介绍了Angular 4 routing – redirectTo with skipLocationChange前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我有一些路由模块,其主路径设置为:/ canvas

const canvasRoutes: Routes = [
    {
        path: "canvas",component: CanvasComponent
    }
];

@NgModule({
    imports: [
        RouterModule.forChild(canvasRoutes)
    ],exports: [
        RouterModule
    ],declarations: [],providers: []
})
export class CanvasRoutingModule {
}

在应用程序路由模块中,我希望每次访问根路径时都将重定向路径设置为/ canvas.目前配置如下:

const appRoutes: Routes = [
    {
        path: "",redirectTo: "/canvas",pathMatch: "full"
    }
];

@NgModule({
    imports: [
        RouterModule.forRoot(appRoutes)
    ],providers: []
})
export class AppRoutingModule {

}

它正常工作,并且对http:// localhost:4201的访问被重定向到http:// localhost:4201 / canvas.

但是,我不希望在重定向后将/ canvas路径附加到URL.怎么能实现这一目标?有没有例如一种方法,我可以将skipLocationChange参数应用于此重定向,因为我将它与router.navigate(… {skipLocationChange:true})一起使用?

解决方法

我已经通过订阅AppComponent中的router.events并手动导航到canvasLocationChange设置为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})
                }
            }
        });
    }
}

猜你在找的Angularjs相关文章