2

I am new to angular & googled for hours to find solutions on lazy route.
https://stackblitz.com/edit/angular-7jmk87

app-routing.module.ts

const routes: Routes = [
  { path: '', redirectTo: 'home', pathMatch: 'full' },
  { path: 'home', component: HomeComponent},
  { path: 'customers', loadChildren: './pages/customers/customers.module#CustomersModule' }
];

customer-routing.module.ts

const routes: Routes = [
    { 
      path: '', 
      component: CustomersComponent, 
    },
    { path: 'detail', component: CustomerDetailComponent },
];

what I want is to redirect to /customers/detail from /customers, but it return an error like Cannot match any routes. URL Segment: 'detail'.
I follow some examples but it still not solved yet.
I hope someone can give a hand to me. Thanks.

3
  • 1
    You can add /customers/ into the routeLink to work around. <a routerLink='/customers/detail'>Detail</a> Commented Sep 29, 2019 at 4:13
  • Do you want to go to customer detail component when clicked on customers link? Commented Sep 29, 2019 at 5:03
  • Notice that lazy routing syntax has changed in Angular 8 Commented Sep 30, 2019 at 10:40

3 Answers 3

3

You can use one of these options:

1) full url to the page

routerLink="/customers/detail"

2) relative url with ./

routerLink="./detail"

3) relative url without slash at the beginning

routerLink="detail"

In two latest options the router will look in the children of the current activated route.

See also RouterLink documentation

Sign up to request clarification or add additional context in comments.

Comments

0

If you want to use Lazy route, then you need to define the customer-routing.moudule as below:-

const routes: Routes = [
    { 
      path: '', 
      component: CustomersComponent, 
    },
    children :[{ path: 'detail', component: CustomerDetailComponent }]    
];

And your router link will be customers/detail in your HTML part.

Hope this will help u.

Comments

0

you can use routerLink to navigate as below:

<a [routerLink]="[ '../detail' ]">Redirect To Detail-1</a>

and

<a [routerLink]="[ 'detail' ]">Redirect To Detail-2</a>

Both of them are correct.

or

In the constructor of customer component inject the following service:

Router, ActivatedRoute

constructor(
  private router: Router,
  private route: ActivatedRoute) { }

Then if you want to redirect to detail component with (click) event , use the following:

<a (click)="redirectToDetail()"> Redirect To Detail </a>
redirectToDetail() {
  this.router.navigate(['../detail'], { relativeTo: this.route })
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.