0

我已经搜索了半天,但我找不到任何有效的东西。

这些是我的路线。

app-routing.module.ts

const routes: Routes = [
  {
    path: '',
    redirectTo: 'feature2',
    pathMatch: 'full',
  },
  {
    path: '',
    canActivate: [MsalGuard],
    loadChildren: (): Promise<FeaturesModule> => import('./features/features.module').then((m) => m.FeaturesModule),
  },
  {
    path: 'auth',
    component: MsalRedirectComponent,
  },
  {
    path: '**',
    component: ErrorPageComponent,
    canActivate: [MsalGuard],
  },
];

features-routing.module.ts

const routes: Routes = [
  {
    path: '',
    component: FullLayoutComponent, // content with sidebar
    canActivate: [AuthGuard],
    children: [
      {
        path: 'feature1',
        loadChildren: (): Promise<Feature1Module> => import('./feature1/feature1.module').then((m) => m.FeatureModule),
      },
      {
        path: 'feature2',
        loadChildren: (): Promise<Feature2Module> => import('./feature2/feature2.module').then((m) => m.FeatureModule),
      },
    ],
  },
  {
    path: '',
    component: ContentLayoutComponent, // content without sidebar
    children: [
      {
        path: 'feature3',
        loadChildren: (): Promise<Feature3Module> => import('./feature3/feature3.module').then((m) => m.FeatureModule),
      },
    ],
  },

app.component.ts有一个router-outletfull-layout-component.ts和 也是如此content-layout-component.ts。当我尝试从feature3(内部feature3)导航到feature2没有任何反应router.navigate('feature2')时。检查跟踪后,我有这个:

Router Event: NavigationStart
    NavigationStart(id: 3, url: '/feature2')
    NavigationStart{id: 3, url: "/feature2", navigationTrigger: "imperative", restoredState: null}id: 3navigationTrigger: "imperative"restoredState: nullurl: "/feature2"__proto__: RouterEvent
Router Event: RoutesRecognized
    RoutesRecognized(id: 3, url: '/feature2', urlAfterRedirects: '/feature2', state: Route(url:'', path:'') { Route(url:'', path:'') { Route(url:'', path:'') { Route(url:'feature2', path:'feature2') { Route(url:'', path:'') { Route(url:'', path:'') }  }  }  }  } )
    RoutesRecognized{id: 3, url: "/feature2", urlAfterRedirects: "/feature2", state: RouterStateSnapshot}
Router Event: GuardsCheckStart
    GuardsCheckStart(id: 3, url: '/feature2', urlAfterRedirects: '/feature2', state: Route(url:'', path:'') { Route(url:'', path:'') { Route(url:'', path:'') { Route(url:'feature2', path:'feature2') { Route(url:'', path:'') { Route(url:'', path:'') }  }  }  }  } )
    GuardsCheckStart{id: 3, url: "/feature2", urlAfterRedirects: "/feature2", state: RouterStateSnapshot}
Event: ChildActivationStart
    ChildActivationStart(path: '')
    ChildActivationStart{snapshot: ActivatedRouteSnapshot}
Router Event: ActivationStart
    ActivationStart(path: '')
    ActivationStart{snapshot: ActivatedRouteSnapshot}

如果我手动导航到feature2它按预期工作。我想我错过了一些东西,但看不到什么。我怎样才能得到这个工作?

4

1 回答 1

1

因为路由相对于当前激活的路由发生。如果你想去父路由,你可以使用完整的 url,如:/root/feature/child

或者您可以只使用相对导航:

假设我们在里面feature并且我们有一个兄弟路线/root/feature1:你可以this.router.navigate(['..', 'feature1']);'../feature1' 完全

于 2021-06-17T18:16:56.553 回答