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

Github

时间: 2024-12-25 20:48:01

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

[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 parameters from one route into another route. There are couple of ways of doing this from the source route perspective: we use the queryParams property in t

[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. We are going to learn how to use a CanDeactivate route guard to ask the user if he really wants to exist the screen, giving the user to for example

Lab - Hot Standby Router Protocol

Topology Objective Configure inter-VLAN routing with HSRP to provide redundant, fault-tolerant routing to the internal network. Background Hot Standby Router Protocol (HSRP) is a Cisco-proprietary redundancy protocol for establishing a fault-tolerant

Node.js笔记(0003)---Express框架Router模块学习笔记

这段时间一直有在看Express框架的API,最近刚看到Router,以下是我认为需要注意的地方: Router模块中有一个param方法,刚开始看得有点模糊,官网大概是这么描述的: Map logic to route parameters. 大概意思就是路由参数的映射逻辑 这个可能一时半会也不明白其作用,尤其是不知道get和param的执行顺序 再看看源码里面的介绍: Map the given param placeholder `name`(s) to the given callbac

[express.js学习笔记]理解Router

(本文内容纯属个人理解,仅供学习探讨) 博主的工作用的是Java Web,私下对JavaScript很感兴趣,也就接触了Node.js,听过Node一般使用Express来搭建Web服务器,就找到了Express,开始阅读文档和例子. 发现官方文档API页面的导航列出了express的几个核心的类(对象): 1. express 2. Application 3. Request 4. Response 5. Router 其中,按照我的理解,express是一个Application对象的工厂

django multidb --- router

之前一篇随笔, 提到了django中怎么使用多数据库, 但是在实际工程中遇到了一个问题,就是admin指定了使用某库, 在测试环境上没问题, 当部署后(库也变动了位置), 修改一个admin的model object保存后就报错. No such table 追溯了下源码, 没有找到问题,  但可以确定的是那个保存操作并没有使用到我们指定的数据库,使用了default. 最后是使用了django的router解决了这个问题 那么django的router是什么? 就是一个类, 定义了如下方法 d

vue router路由(三)

当环境搭建及Vue语法与指令都有所了解,该说下router. build目录是打包配置文件 (不建议动) config是vue项目基本配置文件 dist是构建后文件 js 手动创建 (根据需要) node_modules 根据package.json 安装依赖模块 src资源文件,基本文件都会放在这里 app.vue  父组件 main.js 入口文件 static构建好的文件会在这个目录 index.html 主页 1.首先安装路由通过npm: npm install vue-router 在

Router的创建者——RouteBuilder

Router的创建者--RouteBuilder 在<注册URL模式与HttpHandler的映射关系>演示的实例中,我们总是利用一个RouteBuilder对象来为RouterMiddleware中间件创建所需的Router对象,接下来我们就着重来介绍这个对象.RouteBuilder是我们对所有实现了IRouteBuilder接口的所有类型以及对应对象的统称.[本文已经同步到<ASP.NET Core框架揭秘>之中] 目录一.RouteBuilder二.RouteCollect

[React] React Router: Router, Route, and Link

In this lesson we'll take our first look at the most common components available to us in react-router; Router, Route, and Link. import React from 'react'; import {hashHistory, Route, Router, Link} from 'react-router'; const Home = () => <div><