Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular - URL changed but view not loaded

This is my app-routing-module.ts

const routes: Routes = [
    {
        path: '',
        component: LayoutComponent,
        children: [
            {
                path: 'parent',
                component: ParentComponent,
                children: [
                    { path: ':id/:name', component: ChildComponent }
                ]
            }
        ]
    },
];

I've written a function to check if the URL is working with a static value in parent.component.ts.

goToChild() {
   this.router.navigate(['1/john'], { relativeTo: this.route });
}

And I call the function in the parent.component.html.

<button class="btn btn-primary" (click)="goToChild()">Search</button>

When I click the button, I get the correct URL in the address bar,

 localhost:3000/parent/1/john

But the view never loads, it stays on the parent.component.html. I'm fairly new to Angular and I'm using version 5.

And if I have my routes like this, it works fine.

const routes: Routes = [
    {
        path: '',
        component: LayoutComponent,
        children: [
            {
                path: 'parent',
                component: ParentComponent
            },
            {
                path: 'parent/:id/:name', component: ChildComponent
            }
        ]
    },
];

It feels more appropriate for me to put the ChildComponent route under children array of the ParentComponent, but when I do it. Only the URL changes, not the view.

Any help will be much appreciated.

like image 446
Unknown User Avatar asked Sep 10 '26 10:09

Unknown User


1 Answers

You need to add a <router-outlet></router-outlet> inside the template. That’s how you allow child navigation

like image 188
Hugo Noro Avatar answered Sep 12 '26 01:09

Hugo Noro



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!