[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 ...
 
随机推荐
- jQuery对select标签的常用操作
			
1.获取当前选中项的value. $("#selector").val(); 2.获取当前选中项的text. $("#selector").find(" ...
 - js动画框架设计
			
当你不再依赖JQuery时,当你已经厌倦了引入js类库实现一些动画效果的方式,当你想实现一个简单而实用的动画框架......下面介绍下愚人设计的动画框架:支持动画缓动算法函数,如Linear.Cubi ...
 - 软件工程个人项目-Word frequency program by11061167龚少波
			
(一)工程设计时间预计 1.代码编写:4小时 熟悉Visual studio 2012的使用 : 程序代码部分主要分为三个步骤: (1)主函数的构建,包括各种函数调用及输入输出部分: (2)对目标文件 ...
 - Map/Reduce中Join查询实现
			
张表,分别较data.txt和info.txt,字段之间以/t划分. data.txt内容如下: 201001 1003 abc 201002 1005 def 201003 ...
 - C++Bulder DataSnap 内存泄露元凶
			
DSServerClass1 DSServerClass1DestroyInstance void __fastcall TServerContainer1::DSServerClass1Destro ...
 - 【转】MySQL索引和查询优化
			
原文链接:http://www.cnblogs.com/mailingfeng/archive/2012/09/26/2704344.html 对于任何DBMS,索引都是进行优化的最主要的因素.对于少 ...
 - 导出Excel  Gridview
			
/// <summary> /// 定义导出Excel的函数 /// </summary> /// <param name="FileType ...
 - Linux-Ubuntu 启用root账户
			
Ubuntu Linux有一个与众不同的特点,那就是初次使用时,你无法作为root来登录系统,为什么会这样?这就要从系统的安装说起.对于其他Linux系统来 说,一般在安装过程就设定root密码,这样 ...
 - windows server 2008 支持 .net framework 4.0
			
windows server 2008平台下需要安装sp1,或打KB958854补丁,IIS7.0才能支持.net framework 4.0. 否则,IIS7.0中的应用程序虽然被配置为.net 4 ...
 - branch
			
1.删除分支 git branch -d branch_name error: The branch 'branch_name' is not fully merged. If you are sur ...