【问题标题】:angular routes does now work properly角度路线现在可以正常工作
【发布时间】:2019-02-02 20:08:55
【问题描述】:

我正在编写 Angular 应用程序。在应用程序内有两个文件夹:admin 和 fron-app。 我想要什么,如果输入 localhost:4200/admin ,然后管理模块激活,用户应该看到仪表板(localhost:4200/admin/dashboard);如果用户键入 localhost:4200/app,则 fron-app 模块将激活 (localhost:4200/app/profile)。 现在,当我输入 localhost:4200/app 时会激活管理模块。怎么了?

这是主模块

@NgModule({
  declarations: [
    AppComponent,
  ],
  imports: [
    BrowserModule,
    MDBBootstrapModule.forRoot(),
    HttpClientModule,
    ReactiveFormsModule,
    FormsModule,
    MyDateRangePickerModule,
    RouterModule.forRoot(appRoutes, { useHash: true }),
    NgxPermissionsModule.forRoot(),
    AdminModule
  ],
  schemas: [NO_ERRORS_SCHEMA],
  providers: [DashboardService, StatisticService, OfficeService, UserLdapService,
    CommissionService, AuthService, AuthGuard, GoalService, UserService, {
      provide: HTTP_INTERCEPTORS,
      useClass: AuthInterceptor,
      multi: true,
    }],
  bootstrap: [AppComponent]
})
export class AppModule { }

这是主要路线

export const appRoutes: Routes = [

  {
    path: 'admin',
    loadChildren: 'app/admin/admin.module#AdminModule'
  },
  {
    pathMatch:'full',
    path: 'app',
    loadChildren: 'app/front-app/front-app.module#FrontAppModule'
  },
  {
    path: '',
    redirectTo: '',
    pathMatch: 'full'
  }
];

管理模块

@NgModule({
    declarations: [
        AdminComponent,
        DashboardComponent,
    ],
    imports: [
        BrowserModule,
        HttpClientModule,
        RouterModule.forChild(adminRoutes),
        NgxPermissionsModule.forRoot(),
    ],
    exports:[
        RouterModule
    ],
    schemas: [NO_ERRORS_SCHEMA],
    providers: [DashboardService, StatisticService, OfficeService, UserLdapService,
        CommissionService, AuthService, AuthGuard, GoalService, UserService, {
            provide: HTTP_INTERCEPTORS,
            useClass: AuthInterceptor,
            multi: true,
        }],
})
export class AdminModule { }

管理路线

export const adminRoutes: Routes = [
  {
    path: '',
    component: AdminComponent,
    children: [
      {
        pathMatch: 'full',
        path: '',
        redirectTo: 'dashboard',
      },
      {
        path: 'dashboard',
        component: DashboardComponent,
        canActivate: [AuthGuard]
      },
      {
        path: 'login',
        component: LoginComponent
      }
    ]

  },

];

前端应用路由

export const frontAppRoutes: Routes = [
  {
    path: '',
    component: FrontAppComponent,
    children: [
      {
        path: 'goals',
        component: GoalsComponent
      },
      {
        path: 'login',
        component: FrontLoginComponent
      },
      {
        path: 'profile',
        component: ProfileComponent
      },
    ]

  },

];

【问题讨论】:

    标签: angular angular-routing


    【解决方案1】:

    从路径中删除app并添加redirectTo路径

    Stackblitz Demo

    export const appRoutes: Routes = [
    
      {
        path: 'admin',
        loadChildren: './admin/admin.module#AdminModule'
      },
      {
        pathMatch:'full',
        path: 'app',
        loadChildren: './front-app/front-app.module#FrontAppModule'
      },
      {
        path: '',
        redirectTo: 'app',
        pathMatch: 'full'
      }
    ]
    

    【讨论】:

    • 我更改了如下代码,但应用仍然无法运行
    • 即使我将评论管理路由并转到 localhost:4200/app,管理模块的仪表板也会激活 (localhost:4200/app#/dashboard)
    猜你喜欢
    • 2017-10-20
    • 2016-10-24
    • 1970-01-01
    • 2019-03-18
    • 1970-01-01
    • 1970-01-01
    • 2023-03-11
    相关资源
    最近更新 更多