Angular 5 problems with routes

Asked

Viewed 779 times

1

Guys I have my normal and simple route app.routing.ts

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

import { LoginComponent } from '../login/login.component';
import { HomeComponent } from '../home/home.component';
import { CursosComponent } from '../cursos/cursos.component';
import { CursoDetalheComponent } from '../cursos/curso-detalhe/curso-detalhe.component';
import { PageNotFoundComponent } from '../page-not-found/page-not-found.component';

const PORTAL_ROUTER: Routes = [
// { path: '', redirectTo: '/dashboard', pathMatch: 'full' },
{ path: '', component: HomeComponent },
{ path: 'login', component: LoginComponent },
{ path: 'cursos', component: CursosComponent },
{ path: 'curso/:id', component: CursoDetalheComponent },
{ path: '**', component: PageNotFoundComponent },
];

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

export class PortalRoutingModule {}

the basic routes works localhost/login or localhost/courses, but when I view the route with parameter, in case id, it shows me as follows localhost/courses/course/1 and of course this will give error, because it would have to show localhost/course/1, this is correct way

1 answer

1

You need to pass the absolute path to the route.

Example redirecting by code:

this.router.navigate(['/curso/1'])

Example redirecting by links:

 <a [routerLink]="/curso/1">Link</a>

Browser other questions tagged

You are not signed in. Login or sign up in order to post.