[Angular2 Router] Programmatic Router Navigation via the Router API - Relative And Absolute Router Navigation
In this tutorial we are going to learn how to navigate programmatically (or imperatively) by using the Router API. We are going to learn how to use the function navigateByUrl to navigate using a manually constructed string, but we are also going to learn how to trigger route navigation by using the navigate API method, which takes an array or arguments and a parameters object.
We are going to learn how to do both absolute and relative route navigation using the navigate API, and we are going to introduce the notion of activated route.
In our HerosComponent, we add input box, when you enter the number, it will goes to fetch the hero:
heros.routes.ts:
import {HerosComponent} from "./heros.component"; import {RouterModule} from "@angular/router"; import {HeroComponent} from "./hero/hero.component"; const routes = [ {path: '', component: HerosComponent}, {path: ':id', component: HeroComponent}, ]; export default RouterModule.forChild(routes)
heros.component.html:
Search Index: <input type="text" placeholder="Search" (keyup.enter)="getHeroByIndex(inpRef.value)" #inpRef> <ul> <li *ngFor="let hero of heros | async"> <a [routerLink]="hero.id" routerLinkActive="active" [routerLinkActiveOptions]="{exact: true}">{{hero.name}}</a> </li> <!-- we can also do [routerLink]="['/heros', hero.id]", this will point to "heros/1"; if you do: [routerLink]="['heros', hero.id]", this will point to "heros/heros/1" Since we are already in heros module we just need to do [routerLink]="hero.id", point to "heros/1" --> </ul>
heros.component.ts:
import { Component, OnInit } from '@angular/core'; import {StarWarsService} from "./heros.service"; import {Observable} from "rxjs"; import {Router, ActivatedRoute} from "@angular/router"; @Component({ selector: 'app-heros', templateUrl: './heros.component.html', styleUrls: ['./heros.component.css'] }) export class HerosComponent implements OnInit { heros: Observable<any>; constructor(private starwasService: StarWarsService, private router: Router, private route: ActivatedRoute) { } ngOnInit() { this.heros = this.starwasService.getPeople(); } getHeroByIndex(i){ // this.router.navigateByUrl(`/heros/${i}`); // this.router.navigate(['heros', i]); this.router.navigate([i], {relativeTo: this.route}) } }
So when you type 'enter', will call getHeroByIndex function, there are three ways to nav to a router programmtically.
1. navigateByUrl: it accepts an router url.
2. navigate: first param is an array, absolute path: ['contacts', id] --> contacts/1
3. Recommend: relative path:
'this.route': point to the current router.
[i]: the relative path to the current router.
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· AI技术革命,工作效率10个最佳AI工具
2015-09-27 [Javascript] An Introduction to JSPM (JavaScript Package Manager)