Angular Child Route not working and redirect me to the same page

不问归期 提交于 2019-12-11 09:14:03

问题


Hi guys if you have a spare time please help me with my problem. I cant seem find a possible solution. So I've tried change my route and so far I didn't find any issues but if you find any flaws please let me know, also I try to find any type error and double-check my components, so far I didn't find one but then again let me know. I try to type my Route URL and it work but it displays the same page it wont redirect me to the page that I want. I even delete and install the node module, but unfortunately the problem is still the same. Honestly, I just wanna try if i could still use child route in Angular 6. But anyway Ill provide you route module, my html component and my other components.

First off My route module:

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

import { ContactComponent } from './contact/contact.component';
import { ComponentDetailComponent } from './contact/component-detail/component-detail.component';
import { ContactDescriptionComponent } from './contact/component-detail/contact-description/contact-description.component';

const routes: Routes = [

  {
    path: 'contact',
    component: ContactComponent,
    children: [
        {
            path: ':id',
            component: ComponentDetailComponent,
            children: [
                {
                    path: 'details/:name',
                    component: ContactDescriptionComponent
                }
            ]
        }
      ]
   },

];

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

My app.module.ts:

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

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

import { AppComponent } from './app.component';
import { ContactComponent } from './contact/contact.component';
import { ComponentDetailComponent } from './contact/component-detail/component-detail.component';
import { ContactDescriptionComponent } from './contact/component-detail/contact-description/contact-description.component';

@NgModule({
   declarations: [
     AppComponent,
     ContactComponent,
     ComponentDetailComponent,
     ContactDescriptionComponent
],
imports: [
     BrowserModule,
     AppRoutingModule
],
 providers: [],
 bootstrap: [AppComponent]
})
export class AppModule { }

My app.component.html:

<app-contact></app-contact>
<router-outlet></router-outlet>

My contact.component.html:

<h1>Contact works!</h1>
<a [routerLink]="['id']">Contact Details</a>

My component-detail.component.html:

<h3>Component-detail Works!</h3>
<a [routerLink]="['details','name']" >Description</a>

And lastly my contact-description.component.html:

<h3>Contact-description Works!</h3>

If you have anything would you like me to add up just let me know. Thank You and Good luck


回答1:


You are missing <router-outlet></router-outlet> in component-detail component

See this working DEMO



来源:https://stackoverflow.com/questions/50982077/angular-child-route-not-working-and-redirect-me-to-the-same-page

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!