[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 ...
随机推荐
- Bluebird-Collections
Promise实例方法和Promise类核心静态方法用于处理promise或者混合型(mixed)promise和值的集合. 所有的集合实例方法都等效于Promise对象中的静态方法,例如:someP ...
- Cocos2d-JS v3.0 alpha不支持cocos2d-x的Physics integration
cocos2d-x 3.0新的Physics integration,把chipmunk和Box2D封装到引擎内部 auto scene = Scene::createWithPhysics(); s ...
- 信号量的操作——semop函数
信号量的值与相应资源的使用情况有关,当它的值大于 0 时,表示当前可用的资源数的数量:当它的值小于 0 时,其绝对值表示等待使用该资源的进程个数.信号量的值仅能由 PV 操作来改变. 在 ...
- error日志
2016/06/15 微信调核心时通用意外险 2016-06-15 11:44:23,771>>INFO >> com.isoftstone.core.service.comm ...
- C# winform只有一个进程
在做winform程序的时候,有时候需要客户只能起一个进程,不能起多个进程,用如下代码可以实现. internal static class Program { private static Mute ...
- quartz中关键类
job job是一个接口,你对某一类job的定义,可以通过实现该接口来实现.例如为销售报告定义一个SalesReportJob,包含变量name. job可以使用的几个注解 @DisallowConc ...
- CSS元素水平居中和垂直居中的方法大全
水平居方法: 1.最熟悉的是给元素定义一个宽度,然后使用margin: body{ width:960px; margin:0 auto;}这个是当我们的定义元素的宽度时显现的,如果我们不能定义宽度时 ...
- Android 当媒体变更后,通知其他应用重新扫描
在媒体文件改变后 发出 Intent.ACTION_MEDIA_SCANNER_SCAN_FILE广播,告知其他应用,媒体文件发生改变. 具体代码片段: File oldFile = new File ...
- 查看大图 zoomImage
添加引用 <link rel="stylesheet" media="screen" type="text/css" href=&qu ...
- 消息系统Kafka介绍 - 董的博客
1. 概述 Kafka是Linkedin于2010年12月份开源的消息系统,它主要用于处理活跃的流式数据.活跃的流式数据在web网站应用中非常常见,这些数据包括网站的pv.用户访问了什么内容,搜索了 ...