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.

Github

[Angular2 Router] Programmatic Router Navigation via the Router API - Relative And Absolute Router Navigation的更多相关文章

  1. ASP。netcore,Angular2 CRUD动画使用模板包,WEB API和EF 1.0.1

    下载Angular2ASPCORE.zip - 1 MB 介绍 在本文中,让我们看看如何创建一个ASP.NET Core CRUD web应用程序与Angular2动画使用模板包,web API和EF ...

  2. angular2系列教程(八)In-memory web api、HTTP服务、依赖注入、Observable

    大家好,今天我们要讲是angular2的http功能模块,这个功能模块的代码不在angular2里面,需要我们另外引入: index.html <script src="lib/htt ...

  3. Android UI开发第三十三篇——Navigation Drawer For Android API 7

    Creating a Navigation Drawer中使用的Navigation Drawer的android:minSdkVersion="14",现在Android API ...

  4. 实战SpringCloud响应式微服务系列教程(第九章)使用Spring WebFlux构建响应式RESTful服务

    本文为实战SpringCloud响应式微服务系列教程第九章,讲解使用Spring WebFlux构建响应式RESTful服务.建议没有之前基础的童鞋,先看之前的章节,章节目录放在文末. 从本节开始我们 ...

  5. Gin-Go学习笔记七:Gin-Web框架 布局页面

    模板使用 页面布局 1>     一个html页面由:head部分,body部分,内部css,内部js,外联css,外联的js这几部分组成.因此,一个布局文件也就需要针对这些进行拆分. 2> ...

  6. Gin-Go学习笔记六:Gin-Web框架 Api的编写

    Api编写 1>     Gin框架的Api返回的数据格式有json,xml,yaml这三种格式.其中yaml这种格式是一种特殊的数据格式.(本人暂时没有实现获取节点值得操作) 2>    ...

  7. [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 ...

  8. [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 ...

  9. angular2 学习笔记 ( Router 路由 )

    参考 : https://angular.cn/docs/ts/latest/guide/router.html#!#can-activate-guard https://angular.cn/doc ...

随机推荐

  1. STL源码分析读书笔记--第三章--迭代器(iterator)概念与traits编程技法

    1.准备知识 typename用法 用法1:等效于模板编程中的class 用法2:用于显式地告诉编译器接下来的名称是类型名,对于这个区分,下面的参考链接中说得好,如果编译器不知道 T::bar 是类型 ...

  2. MySql 5.6 慢查询

    网上都巨坑 最后在官网找到了开启方法 原来是配置文件改了 Updated example for 2015 MySQL 5.6: slow_query_log = 1slow_query_log_fi ...

  3. cubieboard中使用py-kms与dnsmasq搭建局域网内全自动KMS激活环境

    众所周知,KMS激活方式是当前广大网民“试用”windows,office的最广泛的激活方式.几乎可以用于微软的全线产品. 但是在本机使用KMS类的激活工具总是有些不放心,一方面每隔180天都要重新激 ...

  4. Ngix 移动端与Pc端 反向代理判断

    如神马搜索和百度(http://www.baidu.com),当用桌面浏览器和移动浏览器访问的结果是不一样的.其中的手段可能有两种: 转载Ngix反向代理判断 服务端直接判断UA输出不同的界面,JAV ...

  5. 随手记录一个 firefox的backgroundPostion-x和-y的问题

    今天帮大师写了一天项目,后来在测试一个显示升序和降序的标签上面,我使用了一个backgroundPosition-y来判断当前icon的状态,却无法不管是使用闭包还是个钟手段,在 firefox下面总 ...

  6. filter 过滤序列

    class filter(object): """ filter(function or None, iterable) --> filter object Ret ...

  7. Java中的BASE64

    located in rt.jar... public class sun.misc.BASE64Encoder extends sun.misc.CharacterEncoder{ //.. } p ...

  8. PetaPoco T4模板修改生成实体

    PetaPoco T4 模板生成的实体类全部包含再一个.CS文件中.通过修改PetaPoco的T4模板,生成单文件实体. 1.生成单CS文件模板: SigleFile.ttinclude <#@ ...

  9. 使用多个Worker的时候Odoo的系统日志配置

    当我们开启Wokrer来启动Odoo的时候,用默认的日志会出现日志丢失的问题,这个是logger的问题:多个进程对单个文件写入日志.有一个简单的解决办法:配置openerp-server.conf,开 ...

  10. 【CSDN博客之星】2013年CSDN博客之星正在评选,希望大家支持,非常感谢!

    首先在此感谢 MoreWindows 秒杀多线程面试题系列让我成长和学习,同时也借鉴了很多优秀观点和示例! 请各位读者可以支持MoreWindows,让更优秀的文章陪伴我们! 各位读者好, 本人博客自 ...