简体   繁体   English

从深层嵌套链接(Angular4)返回父组件后无法导航到子路径

[英]Cannot navigate to child routes after returning to parent component from deeply nested link (Angular4)

I have set up a plunk to demonstrate the problem: 我已经设置了一个插件来演示这个问题:

https://plnkr.co/edit/BSxXBkXUFy3FcJTOf84F?p=preview https://plnkr.co/edit/BSxXBkXUFy3FcJTOf84F?p=preview

Here is the navigation flow => 这是导航流程=>

  1. Click 'Child2 Link' ( which is inside the child1 component ) 单击“Child2 Link” (位于child1组件内)
  2. Click 'Parent Link' ( top level ) 点击“家长链接” (顶级)
  3. Click 'Child1 Link' ( will not work ) 点击“Child1 Link” (不起作用)

For some reason, After following the steps outlined above I will no longer be able to navigate anywhere and the app will crash. 出于某种原因,按照上面列出的步骤后,我将无法再在任何地方导航,应用程序将崩溃。

Here is the necessary code: 这是必要的代码:

PARENT 家长

 <div *ngIf="router.url != '/parent/child2'">
      <ul>
        <li><a routerLinkActive="active" routerLink="child1">Child1</a></li>
      </ul>

      <router-outlet></router-outlet>

 </div>

 <div *ngIf="router.url == '/parent/child2'">
       <router-outlet></router-outlet>
 </div>

CHILD1 CHILD1

  <div>
      <a routerLinkActive="active" routerLink="../child2">Child2</a>
  </div>

ROUTING 路由

const appRoutes: Routes = [
  {path: '', redirectTo: '/parent/child1', pathMatch: 'full'},
  {
    path: 'parent',
    component: ParentComponent,
    children: [
      {path: 'child1', component: Child1Component},
      {path: 'child2', component: Child2Component}
    ]
  }
];

When running this example locally (through the CLI) I get this error: (which for some reason is not appearing on the plunk) 在本地运行此示例(通过CLI)时,我收到此错误:(由于某种原因未出现在plunk上)

ERROR Error: Uncaught (in promise): Error: Cannot activate an already activated outlet
Error: Cannot activate an already activated outlet
    at RouterOutlet.webpackJsonp.../../../router/@angular/router.es5.js.RouterOutlet.activateWith (router.es5.js:5430)
    at ActivateRoutes.webpackJsonp.../../../router/@angular/router.es5.js.ActivateRoutes.activateRoutes (router.es5.js:4578)
    at router.es5.js:4529
    at Array.forEach (<anonymous>)
    at ActivateRoutes.webpackJsonp.../../../router/@angular/router.es5.js.ActivateRoutes.activateChildRoutes (router.es5.js:4529)
    at ActivateRoutes.webpackJsonp.../../../router/@angular/router.es5.js.ActivateRoutes.activateRoutes (router.es5.js:4546)
    at router.es5.js:4529
    at Array.forEach (<anonymous>)
    at ActivateRoutes.webpackJsonp.../../../router/@angular/router.es5.js.ActivateRoutes.activateChildRoutes (router.es5.js:4529)
    at ActivateRoutes.webpackJsonp.../../../router/@angular/router.es5.js.ActivateRoutes.activate (router.es5.js:4422)
    at RouterOutlet.webpackJsonp.../../../router/@angular/router.es5.js.RouterOutlet.activateWith (router.es5.js:5430)
    at ActivateRoutes.webpackJsonp.../../../router/@angular/router.es5.js.ActivateRoutes.activateRoutes (router.es5.js:4578)
    at router.es5.js:4529
    at Array.forEach (<anonymous>)
    at ActivateRoutes.webpackJsonp.../../../router/@angular/router.es5.js.ActivateRoutes.activateChildRoutes (router.es5.js:4529)
    at ActivateRoutes.webpackJsonp.../../../router/@angular/router.es5.js.ActivateRoutes.activateRoutes (router.es5.js:4546)
    at router.es5.js:4529
    at Array.forEach (<anonymous>)
    at ActivateRoutes.webpackJsonp.../../../router/@angular/router.es5.js.ActivateRoutes.activateChildRoutes (router.es5.js:4529)
    at ActivateRoutes.webpackJsonp.../../../router/@angular/router.es5.js.ActivateRoutes.activate (router.es5.js:4422)
    at resolvePromise (zone.js:770)
    at resolvePromise (zone.js:741)
    at zone.js:818
    at ZoneDelegate.webpackJsonp.../../../../zone.js/dist/zone.js.ZoneDelegate.invokeTask (zone.js:424)
    at Object.onInvokeTask (core.es5.js:3924)
    at ZoneDelegate.webpackJsonp.../../../../zone.js/dist/zone.js.ZoneDelegate.invokeTask (zone.js:423)
    at Zone.webpackJsonp.../../../../zone.js/dist/zone.js.Zone.runTask (zone.js:191)
    at drainMicroTaskQueue (zone.js:584)
    at HTMLAnchorElement.ZoneTask.invoke (zone.js:490)

I'm not sure what is causing the crash? 我不确定导致撞车的原因是什么?

I hope the problem is clear! 我希望问题很清楚! Please let me know if you need any more information. 如果您需要更多信息,请与我们联系。

Have you tried changing the if condition so that the router-outlet is not included in it? 您是否尝试更改if条件以使路由器插座不包含在其中? Something like this: 像这样的东西:

 <div *ngIf="router.url != '/parent/child2'">
      <ul>
        <li><a routerLinkActive="active" routerLink="child1">Child1</a></li>
      </ul>
 </div>
 <router-outlet></router-outlet>

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

相关问题 Angular4如何在确保父对象组件变量未定义之后从父组件变量中为其分配变量 - Angular4 how to assign variable in child component from parent component variable after reassuring that it is not undefined Angular4-从父组件到子模式组件的方法调用 - Angular4 - Call a method from parent component to child modal component 将数据从子组件传递到父组件Angular4 - pass data from child component to parent component Angular4 如何在Angular4中将数据从子组件传递到父组件 - How to pass data from child component to parent component in Angular4 Angular4 @Input()将值从父组件传递到子组件 - Angular4 @Input() to pass value from parent component to child component 如何使用angular2中的链接参数数组从子组件导航到上层路由? - How to navigate from child component to upper level routes using link params array in angular2? 无法在angular4中将数据从父组件传递到子组件 - Not able to pass data from parent to child component in angular4 深度嵌套的Angular路线 - Deeply nested Angular routes Angular router.navigate 无法从父组件到子组件工作 - Angular router.navigate not working from parent component to child component Angular 无法导航到嵌套的非子组件 - Angular cannot navigate to nested non-child component
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM