关闭Angular5路由

Turn off Angular 5 routing

我们正在开发 SPA。在某些情况下,它的一部分被渲染到另一个应用程序中(作为标记的一部分)。 在这种情况下,我们不希望在我们的应用程序中进行路由,因为我们可能 "steal" 它来自其他应用程序。 是否有可能 "turn off" 在 Angular 中进行路由?或者忽略未知路由?

我试过了

有效的是,完全删除 RouteModule.forRoot(...)-Import - 但当然在那种情况下,应用程序的其余部分完全被破坏了。

这是 CanLoad-Guard 的一次尝试:

const routes: Routes = [
    // ...
    { path: "", redirectTo: "home", pathMatch: "full" },
    { path: "**", redirectTo: "home" }
];

@NgModule({
    declarations: [],
    imports: [RouterModule.forRoot(routes, { useHash: true,     initialNavigation: false })],
    exports: [RouterModule],
    providers: [{
        provide: "DisableRouting",
        useValue: () => false
    }]
})
export class AppRoutingModule {
    constructor(@Inject("HEADER_RENDER_MODE") private headerRenderMode: boolean, private router: Router) {
        if (!headerRenderMode) {
            router.initialNavigation();
        } else {
            // we don't want routing at this point
            router.resetConfig([{
                    path: "**",
                    component: HomeComponent,
                    canLoad: ["DisableRouting"]
                }]);
        }
    }
}

我找到了一种方法...它并没有真正关闭,更像是忽略它。 在 "turnOff-Mode" 中,我们只定义一个通配符路由到一个组件,它什么也不做,什么也不渲染。

路由模块:

export class AppRoutingModule {
    constructor(@Inject("HEADER_RENDER_MODE") private headerRenderMode: boolean, private router: Router) {
        if (!headerRenderMode) {
            router.initialNavigation();
        } else {
            router.resetConfig([
                { path: "**", component: LazyComponent }
            ]);
        }
    }
}

惰性组件:

@Component({
    selector: "lazy",
    template: ""
})
export class LazyComponent {
}