简体   繁体   English

Angular 2 Router跳过redirectTo并改用子路由

[英]Angular 2 Router skips redirectTo and uses child route instead

I've been tinkering with RC6 for a while now, hoping that finally routing to nested child components would work out of the box. 我一直在修改RC6一段时间,希望最终路由到嵌套的子组件可以使用。 Well, no such luck. 好吧,没有这种运气。 The effect I'm seeing right now is that the redirectTo for the default route is just ignored and instead, Angular goes straight to the first child component. 我现在看到的效果是默认路由的redirectTo仅被忽略,相反,Angular直接转到第一个子组件。

import { Route, RouterModule } from '@angular/router';
import { LandingPageComponent } from 'src/landing-page.component';
import { DepartmentModule } from 'src/department/department.module';

const routes: Route[] = [
  { path: '', redirectTo: 'landing-page', pathMatch: 'full' },
  { path: 'landing-page', component: LandingPageComponent },
  { path: 'department', loadChildren: () => DepartmentModule }
];

export const appRoutingProviders: any[] = [];
export const routing = RouterModule.forRoot(routes, {enableTracing:true});

You should think entering no URL would take me to landing-page , but no, Angular picks department for reasons that are beyond me. 您应该认为不输入URL会将我带到landing-page ,但是不,Angular会选择department ,原因超出了我。

From that point everything works as expected, I can then navigate to a child route 从那时起,一切都会按预期进行,然后我可以导航到子路线

import { Route, RouterModule } from '@angular/router';
import { DepartmentComponent } from './department.component';
import { DepartmentDetailsComponent } from './department-details.component';

const departmentRoutes: Route[] = [
  { path: '', component: DepartmentComponent },
  { path: ':id', component: DepartmentDetailsComponent }
];

export const departmentRouting = RouterModule.forChild(departmentRoutes);

The weird thing here though is, that I need 不过,这很奇怪,我需要

<a [routerLink]="['../..']">

to navigate 1 level back up. 导航到1级备份。 Possibly, both problems are related. 可能两个问题都有关。

Does anyone have an idea what I'm missing? 有人知道我想念的东西吗? The plnkr can be found here. 可以在此处找到plnkr

You just need to remove DepartmentModule from imports ( from AppMoule's @NgModule() ) as shown below, 您只需要从导入从AppMoule的@NgModule() )中删除DepartmentModule ,如下所示,

imports: [ BrowserModule, MdButtonModule, 
           RouterModule, routing]         //<---removed DepartmentModule declaration  

working : http://plnkr.co/edit/qoZ3YCwSz0mQ5o974Dt0?p=preview 工作http : //plnkr.co/edit/qoZ3YCwSz0mQ5o974Dt0?p=preview

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM