简体   繁体   English

Angular 4 routerLink加载错误的组件

[英]Angular 4 routerLink loading wrong component

this is my app-routing.module.ts : 这是我的app-routing.module.ts:

const appRoutes: Routes = [
  {path: '', component: MainComponent, children:[
    {path: '', component: AppComponent, children:[
      {path: 'home', component: HomeComponent},
      {path: 'register', component: RegisterComponent},
      {path: 'login', component: LoginComponent},
    ]},
    {path: 'patient', component: PatientComponent, children: [
        { path: 'patient/profile', component: PatientProfileComponent, outlet: 'patient-outl'},
        { path: 'patient/changepass', component: ChangePasswordComponent, outlet: 'patient-outl'}
        ],
        canActivate: [RoleGuard],
        data: { roles: ['PATIENT'] }
    },
    {path: 'admin', component: AdminComponent, canActivate: [RoleGuard], data: { roles: ['ADMIN'] }},
    {path: 'dentist', component: DentistComponent, canActivate: [RoleGuard], data: { roles: ['DENTIST'] }},
    {path: '**', component: HomeComponent}
  ]}
];

I navigate from click in patient component to changepass like so: 我从患者组件中的单击导航到changepass,如下所示:

<a routerLink="/patient/changepass" routerLinkActive="active">Change Password</a>

but what actually opens up is HomeComponent and not ChangePasswordComponent. 但实际上打开的是HomeComponent,而不是ChangePasswordComponent。 The url is correct - /patient/changepass, but the loaded component is wrong.. Any idea why so? 网址正确-/ Patient / changepass,但是加载的组件错误。。为什么这样?

EDIT: 编辑:

I also tried to navigate like so: 我也尝试这样导航:

<a (click)="editProfile()">Edit Profile</a>

and: 和:

   editProfile(){
     this.router.navigate(['/patient/profile']);
   }

but with the same result - navigation to home component.. 但结果相同-导航到主页组件。

Your child routes are wrong. 您的孩子路线错误。 They should be relative to their parent route. 它们应该相对于其父路径。 Even so the named router outlet not works with non-empty paths. 即使这样,命名路由器出口也不能与非空路径一起使用。 It seems that your issue is related with this one . 看来您的问题与有关。

I prepared a working example here 在这里准备了一个工作示例

I hope this will help. 我希望这将有所帮助。

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

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