【问题标题】:Unable to add a second router-outlet in a parent component.html for showing a nested child in routing无法在父 component.html 中添加第二个路由器出口以在路由中显示嵌套子级
【发布时间】:2021-06-07 20:03:33
【问题描述】:

我有一个 Angular 版本 (11) 应用程序, 一个 app.module,它导入我定义了所有路由的 app.routing.module.ts, 这是路由模块的代码

import { NgModule } from '@angular/core';
import { RouterModule, Routes } from '@angular/router';

import { HomeComponent } from './home/home.component';
import { AboutComponent } from './about/about.component';
import { MainComponent } from './main/main.component';
import { EditOrDeleteComponent } from './main/editOrDelete/edit-or- 
delete.component';

const routes: Routes = [
{path: '', pathMatch: 'full', component: MainComponent},
{
path: 'home', component: HomeComponent, data: { animation: 'home' }, 
children: [
{path: '', component: EditOrDeleteComponent}
]
},
 { path: 'about', component: AboutComponent, data: { animation: 
'about' } },
{ path: '**', redirectTo: ''}
];

@NgModule({
imports: [RouterModule.forRoot(routes)],
exports: [RouterModule]
})

export class AppRoutingModule { }

和 App.module

 import { BrowserAnimationsModule } from '@angular/platform- 
 browser/animations';
 import { NgModule } from '@angular/core';
 import { BrowserModule } from '@angular/platform-browser';

 import { AppRoutingModule } from './app-routing.module';
 import { AppComponent } from './app.component';




  @NgModule({
         declarations: [
         AppComponent,
         ],

        imports: [
          BrowserModule,
          BrowserAnimationsModule,
          AppRoutingModule
          ],
          providers: [],
          bootstrap: [AppComponent]
          })
          export class AppModule { }

在 HomeComponent html 文件中,我需要第二个路由器插座来显示子组件 EditOrDeleteComponent,但我收到“'路由器插座'不是已知元素的错误:

  1. 如果 'router-outlet' 是一个 Angular 组件,则验证它是该模块的一部分。” 每次我尝试添加第二个<router-outlet></router-outlet>, 我试图理解这个问题已经 4 小时了, 谁能帮帮我,

【问题讨论】:

  • 你是否在你的 AppModule 导入[] 数组中添加了“AppRoutingModule”?
  • Zam Abdul Vahid,所有导入和导出都完全正确,只是在将第二个路由器出口添加到父组件的 html 时出现错误
  • 给子路由一个路径,看看它是否有效 {path: 'edit', component: EditOrDeleteComponent}
  • 我尝试了所有可能的方法,但没有运气......
  • 你能用整个应用模块和路由器模块代码更新问题吗

标签: angular routes


【解决方案1】:

您错过了在路由器模块中的声明[]数组中注册组件。

import { NgModule } from '@angular/core';
import { RouterModule, Routes } from '@angular/router';

import { HomeComponent } from './home/home.component';
import { AboutComponent } from './about/about.component';
import { MainComponent } from './main/main.component';
import { EditOrDeleteComponent } from './main/editOrDelete/edit-or- delete.component';

const routes: Routes = [
    {path: '', pathMatch: 'full', component: MainComponent},
    { path: 'home', 
      component: HomeComponent, 
      data: { animation: 'home' }, 
      children: [
          {path: '', component: EditOrDeleteComponent}
      ]
    },
    { path: 'about', 
      component: AboutComponent, 
      data: { animation: 'about' } 
    },
    { path: '**', redirectTo: ''}
];

@NgModule({
    imports: [RouterModule.forRoot(routes)],
    exports: [RouterModule],
    declarations: [HomeComponent, AboutComponent, MainComponent, EditOrDeleteComponent]
})

export class AppRoutingModule { }

Stackblitz - https://stackblitz.com/edit/angular-ivy-to3v9f?file=src/app/app.routing.module.ts

【讨论】:

  • @Riaz 很高兴我能帮上忙。如果您可以将答案标记为已接受,那就太好了,因为这将有助于我们面临类似问题的其他社区成员
  • @Riaz 忽略那些@Inputs(),因为我从我之前的一项工作中分叉了这个项目,以快速显示路由器的变化:)
猜你喜欢
  • 1970-01-01
  • 2017-07-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-07-06
  • 2021-03-05
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多