[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.
[Angular2 Router] Programmatic Router Navigation via the Router API - Relative And Absolute Router Navigation的更多相关文章
- ASP。netcore,Angular2 CRUD动画使用模板包,WEB API和EF 1.0.1
下载Angular2ASPCORE.zip - 1 MB 介绍 在本文中,让我们看看如何创建一个ASP.NET Core CRUD web应用程序与Angular2动画使用模板包,web API和EF ...
- angular2系列教程(八)In-memory web api、HTTP服务、依赖注入、Observable
大家好,今天我们要讲是angular2的http功能模块,这个功能模块的代码不在angular2里面,需要我们另外引入: index.html <script src="lib/htt ...
- Android UI开发第三十三篇——Navigation Drawer For Android API 7
Creating a Navigation Drawer中使用的Navigation Drawer的android:minSdkVersion="14",现在Android API ...
- 实战SpringCloud响应式微服务系列教程(第九章)使用Spring WebFlux构建响应式RESTful服务
本文为实战SpringCloud响应式微服务系列教程第九章,讲解使用Spring WebFlux构建响应式RESTful服务.建议没有之前基础的童鞋,先看之前的章节,章节目录放在文末. 从本节开始我们 ...
- Gin-Go学习笔记七:Gin-Web框架 布局页面
模板使用 页面布局 1> 一个html页面由:head部分,body部分,内部css,内部js,外联css,外联的js这几部分组成.因此,一个布局文件也就需要针对这些进行拆分. 2> ...
- Gin-Go学习笔记六:Gin-Web框架 Api的编写
Api编写 1> Gin框架的Api返回的数据格式有json,xml,yaml这三种格式.其中yaml这种格式是一种特殊的数据格式.(本人暂时没有实现获取节点值得操作) 2> ...
- [Angular2 Router] Optional Route Query Parameters - The queryParams Directive and the Query Parameters Observable
In this tutorial we are going to learn how to use the Angular 2 router to pass optional query parame ...
- [Angular2 Router] CanDeactivate Route Guard - How To Confirm If The User Wants To Exit A Route
In this tutorial we are going to learn how we can to configure an exit guard in the Angular 2 Router ...
- angular2 学习笔记 ( Router 路由 )
参考 : https://angular.cn/docs/ts/latest/guide/router.html#!#can-activate-guard https://angular.cn/doc ...
随机推荐
- iOS优秀博客收录(持续更新)
唐巧 王巍 破船之家 NSHipster Limboy 无网不剩 念茜的博客 Xcode Dev Ted’s Homepage txx’s blog KEVIN BLOG 阿毛的蛋疼地 亚庆的 Blo ...
- 【文件系统】浅解释FAT32
了解完linux下的文件系统之后,顺便对FAT32也研究一下. 假如一个FAT32表如下所示. 文件的簇应该保留在目录中,根据此簇,应该能得到一个块. 要找到文件的下一块,就要根据簇在FAT中寻找,所 ...
- 多台服务器最好加上相同的machineKey
<machineKey validationKey="6E993A81CF4BDCA1C1031528F55DADBB8AF1772A" decryptionKey=&q ...
- dom 冒泡事件
<!doctype html> <html> <head> <meta charset="utf-8"> <style> ...
- HTTP Status 500 - Servlet.init() for servlet htmlWebConfig threw exception
HTTP Status 500 - Servlet.init() for servlet htmlWebConfig threw exception
- Codeforces Round #367 (Div. 2) D. Vasiliy's Multiset (0/1-Trie树)
Vasiliy's Multiset 题目链接: http://codeforces.com/contest/706/problem/D Description Author has gone out ...
- Codeforces Round #368 (Div. 2) B. Bakery (模拟)
Bakery 题目链接: http://codeforces.com/contest/707/problem/B Description Masha wants to open her own bak ...
- SpriteKitCommonUse
[SpriteKitCommonUse] 1.SKView中提供了显示FPS和NodeCount(当前view)的方法,如下: 展现一个scene: - (void)viewWillAppear:(B ...
- HDU 1847 Good Luck in CET-4 Everybody!(找规律,或者简单SG函数)
Good Luck in CET-4 Everybody! Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K ...
- js 控制 table style css
var table = objj.getElementsByTagName('table'); alert(table[i].width); if(table==null) return; for(v ...