I have this router:
import { Route, RouterModule } from '@angular/router';
import { ProjectDetailsComponent } from '../components/project-details/project-details.component';
export const ROUTES: Route[] = [
{
path: '',
redirectTo: '/details',
pathMatch: 'full'
},
{
path: 'details/:id',
component: ProjectDetailsComponent
}
];
Also I have an appropriate controller, named ProjectDetailsComponent.
This is my app module, in which I have registered my routes:
import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { RouterModule } from '@angular/router';
// Custom components
import { AppComponent } from './app.component';
import { ProjectDetailsComponent } from './components/project-details/project-details.component';
// Routes
import { ROUTES } from './services/router';
@NgModule({
imports: [
BrowserModule,
RouterModule.forRoot(ROUTES, { useHash: true })
],
declarations: [AppComponent, ProjectDetailsComponent],
bootstrap: [AppComponent]
})
export class AppModule {
}
And its my app.component, in which I try to redirect to ProjectDetailsComponent using "/details" routes and send to it project id:
import { Component, OnInit } from '@angular/core';
import { IProject } from './domain/projects
import { Router } from '@angular/router';
// Routes
import { ROUTES } from './services/router';
@Component({
selector: 'projects-app',
templateUrl: 'app/app.component.html'
})
export class AppComponent implements AfterViewInit {
public activeProject: IProject;
public projects: Array<IProject>;
constructor(public router: Router) {
}
ngOnInit() {
this.router.navigate(['/details', this.activeProject.Id]);
}
}
But when I try to navigate to 'details/:id' router I get following error:
C:\Sources\SolarPro\build\debug\resources\app\node_modules\@angular\core\bundles\core.umd.js:3521 EXCEPTION: Uncaught (in promise):
Error: Cannot match any routes. URL Segment: 'details'
Please help me with my problem.
The redirect is going to /details which doesn't have a component defined. You need to define a component for /details.
routes.ts
export const ROUTES: Routes = [
{
path: '',
redirectTo: '/details',
pathMatch: 'full'
},
{
path: 'details',
component: DetailsComponent
},
{
path: 'details/:id',
component: ProjectDetailsComponent
}
];
details.component.ts
@Component({
selector: 'app-details',
templateUrl: './details.component.html',
styleUrls: ['./details.component.css']
})
export class DetailsComponent implements OnInit {
constructor(private router: Router) { }
ngOnInit() {
this.router.navigate(['/details', 1]);
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With