Route parameters are placeholders that we set in the web address of our app. It allows us to pass information through the web address.
Route parameters are placeholders that we set in the web address of our app. It allows us to pass information through the web address.
www.codeible.com/tutorial/294
Optional route parameters are added as part of the web address when we transition from one route to another.
www.codeible.com/...;theme=dark
Let’s see how they work in code, open your Angular project on VS code.
ng generate component RoutingExample
Make sure to have the router-outlet tags in app component html page.
<router-outlet></router-outlet>
Go to the app routing module and declare a route, use exampleRoute/name for the path and the RoutingExample component for the component.
const routes: Routes [] = [
If we take a look at the path, the path is static. We must go to localhost:4200/exampleRoute/name to activate the route.
const routes: Routes [] = [
Instead of using static paths, we can add a parameter to the path and create a custom syntax for our route.
const routes: Routes [] = [
Retrieving the Parameter Value
import { ActivatedRoute } from '@angular/router';
Inside the object, there is a property call params that we can subscribe to. It returns an object with all the parameters.
Since our parameter is call name, we can get the value by grabbing the paramsObject and accessing the name property.
If we take a look at the ExampleRoute html page. We’re displaying a static message. Replace it with an interpolation statement and pass in a variable.
<p>{{name}}</p>
Go back to the component TypeScript file and declare the variable. Then Assign it with the parameter value.
import { ActivatedRoute } from '@angular/router';
Now when we go to localhost:4200/exampleRoute/whatever, it will display whatever value we placed in parameter of the path.
Optional Parameters
The paramsObject in the params subscription also contains all the optional parameters So we can grab the paramsObject and access the age and gender parameters.
Declare 2 variables to store the age and gender value and then go to the ExampleRoute component html page and insert the age and gender.
import { ActivatedRoute } from '@angular/router';
<p>{{name}}</p>
Save the project and go to the browser.
Add a button and label it Optional Params in the app component html page. Then add the routerLink attribute directive.
<router-outlet></router-outlet>
Normally to transition to another route, we would put the path that we want. But in order to add the parameters, we need to use the compact syntax and provide an array.
<button [routerLink]="[]">Optional Params</button>
Each element in the array represents an extension. So we can put exampleRoute and John to represent exampleRoute/John.
<button [routerLink]="['exampleRoute', 'John', { age: 10, gender: 'male'} ]">Optional Params</button>
Using the Router object
Instead of using the routerLink attribute directive, we can also use the router object. Go back to VS Code and add a click event to the button and give it a function to call.
<button
Go to the app component TypeScript file. Add the router import and inject it into the constructor.
optionalParams() {
The navigate method takes in an array that works the same way as our routerLink attribute so we can copy the contents and paste it inside the array.
optionalParams() {
<button (click)="optionalParams()">
As you can see it worked the same way.
Recap