I'm trying to get it that when the user types in a URL (http://localhost:4200/directory/test) it will show 'test' in the view using two-way data binding. I only get an error when I add a string to the end of the URL
ERROR : EXCEPTION: Uncaught (in promise): Error: Cannot match any routes. URL Segment: 'test' Error: Cannot match any routes. URL Segment: 'test'
HTML
<p>
Directory View
</p>
<p>{{sampleurl}}</p>
Directory Component
import { Component, OnInit } from '@angular/core';
import { ActivatedRoute } from '@angular/router';
@Component({
selector: 'app-directory',
templateUrl: './directory.component.html',
styleUrls: ['./directory.component.css']
})
export class DirectoryComponent implements OnInit {
sampleurl: string;
// get a snapshit of the current sampleurl URL and display those detials in the view
constructor(private route: ActivatedRoute) {
this.sampleurl = route.snapshot.params['sampleurl'];
}
ngOnInit() {
}
}
My routes file
import { Routes, RouterModule } from '@angular/router';
import { DirectoryComponent } from './directory/directory.component';
import { HomeComponent } from './home/home.component';
const APP_ROUTES: Routes = [
{ path: '/',
component: HomeComponent
},
{
path: '/directory',
component: DirectoryComponent,
},
{
path: '/directory/:sampleurl',
component: DirectoryComponent,
}
];
export const APP_ROUTES_PROVIDER = RouterModule.forRoot(APP_ROUTES);
App Module
import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { FormsModule } from '@angular/forms';
import { RouterModule, Routes } from '@angular/router'; // This is what we need for routing
import { AppComponent } from './app.component';
import { HomeComponent } from './home/home.component';
import { DirectoryComponent } from './directory/directory.component';
const appRoutes: Routes = [
{ path: 'directory', component: DirectoryComponent},
{ path: '', component: HomeComponent}
];
@NgModule({
declarations: [
AppComponent,
HomeComponent,
DirectoryComponent
],
imports: [
BrowserModule,
FormsModule,
RouterModule.forRoot(appRoutes) // Add here array with paths and components
],
providers: [],
bootstrap: [AppComponent]
})
export class AppModule { }