【问题标题】:Angular 5 Routing issue on child route子路由上的Angular 5路由问题
【发布时间】:2023-03-27 22:35:01
【问题描述】:

我有一个名为 User 的路由应用程序,然后我有一个名为 UserDetails 的子路由,当我单击编辑时,我想导航到新路由(userdetails),我收到错误无法匹配任何路由。 URL 段:'user/1'。下面是我的路由配置

异常结果 http://localhost:4200/user - 网格页面 http://localhost:4200/user/1 - 编辑页面

app.routing.ts

export const routes: Routes = [
  {
    path: '',
    redirectTo: 'dashboard',
    pathMatch: 'full',
  },
  {
    path: '',
    component: FullLayoutComponent,
    data: {
      title: 'Home'
    },
    children: [
      {
        path: 'dashboard',
        loadChildren: './views/dashboard/dashboard.module#DashboardModule'
      },
      {
        path: 'user',
        loadChildren: './views/user/user.module#UserModule',
      } 
    ]
  },
  {
    path: 'login',
    component: SimpleLayoutComponent,
    data: {
      title: ''
    },
    children: [
      {
        path: '',
        loadChildren: './views/login/login.module#LoginModule',
      }
    ]
  }
];

user-routing.module.ts

const routes: Routes = [
  {
    path: '',
    component: UserComponent,
    data: {
      title: 'User'
    },
    children: [
      {
        path: 'user/:id',
        component: UserDetailsComponent,
        data: {
          title: 'User Details'
        }
      }
    ]
  }
];

【问题讨论】:

    标签: angular routing angular2-routing


    【解决方案1】:

    您可以对您的 user-routing.module.ts 进行如下更改。

    const routes: Routes = [
     {
       path: '',
       component: UserComponent,
       data: {
         title: 'User'
       },
       children: [{
         path: 'User',
         component: UserComponent,
         pathMatch: 'full',
       },{
         path: 'user/:id',
         component: UserDetailsComponent,
         data: {
          title: 'User Details'
        }
      }
      ]
     }
    

    ];

    点击编辑时,调用函数示例

    import {Component, OnInit, ViewChild} from '@angular/core';
    import {ActivatedRoute, Router} from '@angular/router';
    constructor(private router: Router,
              private route: ActivatedRoute){
    }
    editUserData(){
      this.router.navigate([id],{relativeTo: this.route});
      return false;
    }
    

    【讨论】:

    • @Kavitha,也没有工作得到相同的错误错误错误:未捕获(承诺):错误:无法匹配任何路由。 URL 段:'user/1' 错误:无法匹配任何路由。 URL 段:'user/1' at ApplyRedirects.noMatchError (router.js:1765) at CatchSubscriber.eval [as selector] (router.js:1730) at CatchSubscriber.error (catchError.js:105) at MapSubscriber.Subscriber。 _error (Subscriber.js:131) 在 MapSubscriber.Subscriber.error (Subscriber.js:105) 在 MapSubscriber.Subscriber._error (Subscriber.js:131)
    【解决方案2】:

    在你的 user-routing.module.ts 中,尝试使用 ":id" 而不是 "user/:id"

    const routes: Routes = [
      {
        path: '',
        component: UserComponent,
        data: {
          title: 'User'
        },
        children: [
          {
            path: ':id',
            component: UserDetailsComponent,
            data: {
              title: 'User Details'
            }
          }
        ]
      }
    ];
    

    【讨论】:

    • 没有运气 URL 更改为例外,但未加载 UserDetailsComponent。有什么想法吗?
    • 导航到“用户”时是否加载 UserComponent?
    • 不,我在我的 UserComponent 构造函数中创建了一个控制台日志,它从未执行过
    • 好的。让我们先让路径“用户”工作。也许您的 UserModule 未正确加载。您可以在主组件中注入“私有路由:ActivatedRoute”,并查看是否配置了所有路由(子路由)。前任。 route.children
    • 我的用户模块工作正常,UserDetailsComponent 有问题
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-08-10
    • 2018-09-23
    • 2023-03-06
    • 2018-08-30
    • 2018-09-21
    • 2016-12-19
    • 1970-01-01
    相关资源
    最近更新 更多