javascript - 子路由作为 Angular 主路由上的默认路由

标签 javascript angular

我有一个 Angular 6 应用程序,并且有一条包含子路线的路线。但如果你走主路线,你就必须走主路线的第一条子路线。像这样:

http://localhost:4200/instellingen/account

但现在是这样的:

http://localhost:4200/instellingen/instellingen/account

我尝试了很多,也用谷歌搜索了很多

所以我在 app.routes.ts 文件中有这个:

{
     path: 'instellingen',    
     component: SettingsNavigationComponent,
     canActivate: [AuthGuard],
     children: [
      {path: '', redirectTo: 'instellingen/account', pathMatch: 'full' },
      {path: 'instellingen/account', component: SettingsAccountComponent },
      {path: 'apparaten' , component: SelfcareComponent },
      {path: 'apparaten' , component: SelfcareComponent },
      {path: 'apps' , component: SettingsAppsComponent },
      {path: 'indicatiepermissies' , component: SettingsIndicatorPermissionsComponent },
      {path: 'algemeen' , component: SettingsGeneralComponent },
      { path: 'algemeen', component: SettingsGeneralComponent },
      {path: 'log' , component: SettingsLogComponent },
     ]
   },

谢谢

Thank you. but then I get this error: Error: Invalid configuration of route '{path: "instellingen/", redirectTo: "account"}': please provide 'pathMatch'. The default value of 'pathMatch' is 'prefix', but often the intent is to use 'full'.
    at validateNode (router.js:554)
    at validateConfig (router.js:515)
    at validateNode (router.js:560)
    at validateConfig (router.js:515)
    at Router.push../node_modules/@angular/router/fesm5/router.js.Router.resetConfig (router.js:3546)
    at new Router (router.js:3427)
    at setupRouter (router.js:5385)
    at _callFactory (core.js:9291)
    at _createProviderInstance$1 (core.js:9237)
    at initNgModule (core.js:9170)

这就是导航组件的样子:

<nav class="nav-tab-bar">
  <a class="nav-tab-bar-tab" routerLinkActive="active" routerLink="/instellingen/account">Account</a>
  <a class="nav-tab-bar-tab" routerLinkActive="active" routerLink="/instellingen/apparaten">Apparaten</a>
  <a class="nav-tab-bar-tab" routerLinkActive="active" routerLink="/instellingen/apps">Apps</a>
  <a class="nav-tab-bar-tab" routerLinkActive="active" routerLink="/instellingen/indicatiepermissies">Meldingen</a>
  <a class="nav-tab-bar-tab" routerLinkActive="active" routerLink="/instellingen/algemeen">Gegevens toegang</a>
  <a class="nav-tab-bar-tab" routerLinkActive="active" routerLink="/instellingen/log">Log</a>

</nav>
<router-outlet></router-outlet>

最佳答案

遵循以下更改

{
 path: 'instellingen',    
 component: SettingsNavigationComponent,
 canActivate: [AuthGuard],
 children: [
  { path: '', redirectTo: 'account', pathMatch: 'full' },
  { path: 'account', component: SettingsAccountComponent },
  { path: 'apparaten' , component: SelfcareComponent },
  { path: 'apparaten' , component: SelfcareComponent },
  { path: 'apps' , component: SettingsAppsComponent },
  { path: 'indicatiepermissies' , component: SettingsIndicatorPermissionsComponent },
  { path: 'algemeen' , component: SettingsGeneralComponent },
  { path: 'algemeen', component: SettingsGeneralComponent },
  { path: 'log' , component: SettingsLogComponent },
 ]
},

关于javascript - 子路由作为 Angular 主路由上的默认路由,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57056498/

相关文章:

javascript - 如何将项目插入数组而不删除最后一个元素

javascript - Node.js - 如果我在 I/O 和回调仍在运行时调用 response.end 会发生什么?

JavaScript document.write 未出现在页面源代码中

javascript - 如何将 DOM 状态重置为初始状态(首次收到页面时?)

Angular 2点击在ngFor内多次触发

angular - 根据每个对象中的字段过滤对象数组

javascript - Uncaught Error : Can't resolve all parameters for 'ComponentName' : ([object Object],?)

javascript - 将请求发送到页面,不执行成功函数

angular - 在 Ionic 2 的页面之间传递数据

forms - Angular 2 跨域验证(基于模型)addErrors?