Angular 8路由无法正确导航

时间:2019-12-09 15:38:28

标签: angular-routing angular8

我已经为我的应用程序中名为client.module.ts的客户端工作创建了一个单独的路由模块,并在app.module中调用了它。但是由于导航到路线后的某些问题,该组件的内容未显示。控制台什么也没显示...自从我将其放在app.component.html中以来,只是脚注即将到来,这是我的app.module设置

import { NgModule } from '@angular/core';
import { HttpClientModule } from '@angular/common/http';

import { AppRoutingModule } from './app-routing.module';
import { ClientModule } from './client/client-routing/client.module';
import { AppComponent } from './app.component';
import { HomeComponent } from './client/home/home.component';
import { FooterComponent } from './client/footer/footer.component';
import { FeaturedBlogComponent } from './client/featured-blog/featured-blog.component';
import { PageNotFoundComponent } from './page-not-found/page-not-found.component';



@NgModule({
  declarations: [
    AppComponent,
    HomeComponent,
    FooterComponent,
    FeaturedBlogComponent,
    PageNotFoundComponent

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

我的client-routing.module.ts文件如下

import { Routes, RouterModule } from '@angular/router';
import { BlogListComponent } from '../blog-list/blog-list.component';
import { BlogDetailComponent } from '../blog-detail/blog-detail.component';
import { AboutComponent } from '../about/about.component';
import { ContactComponent } from '../contact/contact.component';

const routes: Routes = [
  {path: 'blogs', component: BlogListComponent},
  {path: 'blog/:_id', component: BlogDetailComponent},
  {path: 'about', component: AboutComponent},
  {path: 'contact', component: ContactComponent}
];

@NgModule({
  imports: [RouterModule.forChild(routes)],
  exports: [RouterModule]
})
export class ClientRoutingModule { }

下面是我要导入到app.module中的client.module.ts文件

import { CommonModule } from '@angular/common';

import { ClientRoutingModule } from './client-routing.module';
import { BlogListComponent } from '../blog-list/blog-list.component';
import { BlogDetailComponent } from '../blog-detail/blog-detail.component';
import { AboutComponent } from '../about/about.component';
import { ContactComponent } from '../contact/contact.component';


@NgModule({
  declarations: [BlogListComponent, BlogDetailComponent, AboutComponent, ContactComponent],
  imports: [
    CommonModule,
    ClientRoutingModule
  ]
})
export class ClientModule { }

您还可以检查我的app.component.html如下

<app-home *ngIf="router.url == '/'"></app-home>
<app-featured-blog *ngIf="router.url == '/'"></app-featured-blog>
<router-outlet></router-outlet>
<app-footer></app-footer>

app-routing.module.ts

import { NgModule } from '@angular/core';
import { Routes, RouterModule } from '@angular/router';
import { PageNotFoundComponent } from './page-not-found/page-not-found.component';
const routes: Routes = [
{path: '', redirectTo: '', pathMatch: 'full'},
{path: '**', component: PageNotFoundComponent}
];
@NgModule({
imports: [RouterModule.forRoot(routes)],
exports: [RouterModule]
})
export class AppRoutingModule { }

每当我尝试访问自己创建的路由时,只会加载页脚,而不会加载其他任何内容。主页工作正常。

2 个答案:

答案 0 :(得分:0)

对不起,我无法在问题上发表评论。.我想知道根模块如何知道使用子路由。您是否不需要告诉根路由路径何时加载子路由?我所有的子路线都使用这样的行加载:

{ path: '', loadChildren: () => import('./client/client-routing/client.module').then(m => m.ClientModule) }

答案 1 :(得分:0)

通过在 NgModule 中添加“enableTracing: true”来启用路由跟踪

imports: [RouterModule.forRoot(routes, { enableTracing: true })]

有助于解决路由器相关问题