简体   繁体   English

Angular 子组件路由失败?

[英]Angular child component routing is failing?

I have admin page where i have side-navbar where user have add/edit operations, Now when user clicks on add then add component should display in router-oulter that is not happening, any help?我有管理页面,我有侧导航栏,用户可以在其中进行添加/编辑操作,现在当用户单击add时,添加组件应该显示在没有发生的 router-oulter 中,有什么帮助吗?

admin.component.html admin.component.html

<div class="container-fluid sidebarContent">
  <div class="row">
    <div class="col-3">
      <nav class="navbar navbar-inverse navbar-fixed-top" id="sidebar-wrapper" role="navigation">
        <ul class="nav sidebar-nav">
            <li>
              <a class="nav-link" routerLink="['/add']" routerLinkActive="active">Add</a>
            </li>
            <li>
              <a class="nav-link" routerLink="['/edit']" routerLinkActive="active">Edit</a>
            </li>
        </ul>
      </nav>
    </div>
    <div class="col-9">
      <router-outlet></router-outlet>
    </div>
  </div>
</div>

app.routing.module.ts app.routing.module.ts

import { NgModule } from '@angular/core';
import { Routes, RouterModule } from '@angular/router';
import { AdminComponent } from './_components/admin/admin.component';
import { AddComponent } from './_components/admin/add/add.component';


  const routes: Routes = [
    { path: 'admin', component: AdminComponent,
    children: [
      {
        path: "add",
        component: AddComponent
      }
    ]
   },
    { path: '', redirectTo: '/home', pathMatch: 'full' }
  ];

@NgModule({
  imports: [RouterModule.forRoot(routes, { relativeLinkResolution: 'legacy' })],
  exports: [RouterModule]
})
export class AppRoutingModule { }

From what I can see you only have one route which is /admin/add.据我所知,您只有一条路线,即 /admin/add。 You try to route to /add where there is no matching route.您尝试路由到没有匹配路由的 /add 。

Remove the leading slash in the routerLink , otherwise, it's considered as absolute.删除routerLink中的前导斜杠,否则,它被认为是绝对的。 Source: https://angular.io/api/router/RouterLink#relative-link-paths来源: https://angular.io/api/router/RouterLink#relative-link-paths

Also if you want the routerLink content to be evaluated as an expression, surround it with square brackets: [routerLink]="['add']" , or just routerLink="add"此外,如果您希望将routerLink内容作为表达式进行评估,请用方括号括起来: [routerLink]="['add']" ,或者只是routerLink="add"

Your navigation links are telling Angular to look for either an /add route or an /edit route.您的导航链接告诉 Angular 寻找/add路线或/edit路线。 However, in your Routes array in app-routing.module.ts , only the /admin , /home , and '' (empty) path exist at the main routing level.但是,在app-routing.module.ts的 Routes 数组中,主路由级别仅存在/admin/home'' (空)路径。 Angular cannot find add or edit since they are children to the /admin route. Angular 找不到添加或编辑,因为它们是/admin路由的子级。

You can either move the add and edit routes up one level in the route tree like this:您可以在路由树中将添加和编辑路由上移一级,如下所示:

const routes: Routes = [
  { path: 'admin', component: AdminComponent },
  { path: "add", component: AddComponent },
  { path: "edit", component: EditComponent },
  { path: 'home', component: HomeComponent, },
  { path: '', redirectTo: '/home', pathMatch: 'full' }
];
<nav class="navbar navbar-inverse navbar-fixed-top" id="sidebar-wrapper" role="navigation">
  <ul class="nav sidebar-nav">
    <li>
      <a class="nav-link" [routerLink]="['/add']" routerLinkActive="active">Add</a>
    </li>
    <li>
      <a class="nav-link" [routerLink]="['/edit']" routerLinkActive="active">Edit</a>
    </li>
  </ul>
</nav>

or you can add another <router-outlet></router-outlet> into admin.component.html and change your routerLink to this:或者您可以将另一个<router-outlet></router-outlet>添加到admin.component.html并将您的 routerLink 更改为:

<p>admin works!</p>
<router-outlet></router-outlet>
<nav class="navbar navbar-inverse navbar-fixed-top" id="sidebar-wrapper" role="navigation">
  <ul class="nav sidebar-nav">
    <li>
      <a class="nav-link" [routerLink]="['/admin/add']" routerLinkActive="active">Add</a>
    </li>
    <li>
      <a class="nav-link" [routerLink]="['/admin/edit']" routerLinkActive="active">Edit</a>
    </li>
  </ul>
</nav>

In this snippet, if we look at the /admin/add route for example, Angular will evaluate the route and resolve the admin portion of the route (the name in between the first and second / ) and it will know to look for a child 'add' route (because of the second / .在这个片段中,如果我们以/admin/add路由为例,Angular 将评估路由并解析路由的admin部分(第一个和第二个/之间的名称),它会知道要寻找一个孩子'add' 路线(因为第二个/

Here is a github repo with the example in case I was a little confusing.这是一个带有示例的github 存储库,以防我有点困惑。

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

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