【发布时间】:2020-04-02 09:54:07
【问题描述】:
所以,我有一个 Angular 8 应用程序,它由以下项目结构组成:
app-routing.module.ts 包含延迟加载模块auth 和dashboard 的主要路由。 app.component 包含发生主路由的router-outlet。这工作正常。
但是,我想要的是有一个 dashboard.page 将包含导航栏、侧边栏等组件,另一个 router-outlet 将根据辅助路由更改组件。例如,/dashboard 应该带我到dashboard.page,因为app.component 中的主路由器。 /dashboard/new 应该带我到 dashboard.page 并且在其中,子路由器插座上应该有 temp.screen,这将根据我在侧边栏上单击的内容而改变。
到目前为止,当我延迟加载模块时,这不起作用。如果我急切地自己加载页面组件,那么整个流程就可以完美运行。即使在延迟加载的格式中,有没有办法让它工作?
dashboard-routing.module.ts
import { NgModule } from "@angular/core";
import { RouterModule, Routes } from "@angular/router";
import { CommonModule } from "@angular/common";
import { DashboardPage } from "./page/dashboard.page";
import { TempTwoScreen } from "./screens/temp-two/temp-two.screen";
import { TempScreen } from './screens/temp/temp.screen';
const routes: Routes = [
{
path: "",
component: DashboardPage,
children: [
{
path: "",
component: TempScreen,
outlet: "dboard",
},
{
path: "new",
component: TempTwoScreen,
outlet: "dboard",
},
],
},
];
@NgModule({
imports: [CommonModule, RouterModule.forChild(routes)],
exports: [RouterModule],
})
export class DashboardRoutingModule {}
app-routing.module.ts
import { NgModule } from "@angular/core";
import { Routes, RouterModule } from "@angular/router";
const APP_ROUTES: Routes = [
{
path: "",
loadChildren: () => import("./modules/auth/auth.module").then((m) => m.AuthModule),
},
{
path: "dashboard",
loadChildren: () => import("./modules/dashboard/dashboard.module").then((m) => m.DashboardModule),
},
];
@NgModule({
imports: [RouterModule.forRoot(APP_ROUTES, { enableTracing: true, relativeLinkResolution: "corrected" })],
exports: [RouterModule],
})
export class AppRoutingModule {}
dashboard.module.ts
import { NgModule } from "@angular/core";
import { CommonModule } from "@angular/common";
import { IndexScreen } from "./screens/index/index.screen";
import { DashboardRoutingModule } from "./dashboard-routing.module";
import { DashboardPage } from "./page/dashboard.page";
import { TempScreen } from "./screens/temp/temp.screen";
import { TempTwoScreen } from "./screens/temp-two/temp-two.screen";
import { FormsModule } from '@angular/forms';
@NgModule({
declarations: [
DashboardPage,
TempTwoScreen,
TempScreen,
IndexScreen,
],
imports: [CommonModule, DashboardRoutingModule, FormsModule],
exports: [],
providers: [],
})
export class DashboardModule { }
【问题讨论】:
-
能否展示以下文件的代码:app-routing.module.ts、dashboard.module.ts、dashboard-routing.module.ts?
-
@anymeshsingh 添加。
-
你的代码很好,你只需要在dashboard.component.html中添加另一个'router-outlet',它将加载仪表板中的所有子路由,如'/dashboard/temp'或'/dashboard/温度2'。 '/dashboard' 之后的 '/temp' 和 '/temp2' 将加载位于仪表板组件内的路由器插座内的 temp 组件和 temp2 组件。
-
所以我在我的dashboard.page.html 中添加了另一个路由器出口,它应该加载子路由,但这仅适用于空路由上的第一条路由。我通过 routerlink 访问的任何其他链接都会破坏整个代码,说它找不到路由。
-
您的意思是对于“/dashboard”,您将转到 Dashboard 内部的 TempScreen,但对于路径“/dashboard/new”,您不会转到 TempTwoScreen。您可以尝试直接从 URL 转到相同的路线,即“/dashboard/new”,如果它有效,则意味着您为“routerLink”提供了不正确的路径。尝试提供类似 routerLink="/dashboard/new" 的内容。但它不起作用,然后让我知道我将分享其中一个项目的代码,向您展示它是如何为我工作的。
标签: angular