文章目录

标签routerLink路由传递参数

url中get传值

// queryParams 传递的是一个对象
<a [routerLink]="[ '/endpage/']" [queryParams]="{name:'huangbiao',age:30}">endpage</a>

定义路由

const routes: Routes = [
{ path: 'endpage', component: EndPageComponent }
];
url的地址是 http://localhost:4200/endpage?name=huangbiao&age=30

获取参数

import { Component, OnInit } from '@angular/core';
import {ActivatedRoute} from '@angular/router'; @Component({
selector: 'app-end-page',
templateUrl: './end-page.component.html',
styleUrls: ['./end-page.component.scss']
})
export class EndPageComponent implements OnInit {
constructor(public route:ActivatedRoute) { } ngOnInit() {
console.dir(this.route);
debugger
this.route.params.subscribe((data)=>{
console.log(data); // 打印的是一个对象,key为name和age
});
} }

获取参数要依赖注入 route:ActivatedRoute对象

配置动态路由

// 第一个参数对应路由的name,第二个参数对应路由的age
<a [routerLink]="[ '/endpage/', 'huangbiao','30' ]">endpage</a>

定义路由

const routes: Routes = [
{ path: 'endpage/:name/:age', component: EndPageComponent }
];
url地址是 http://localhost:4200/endpage/huangbiao/30

获取参数

import { Component, OnInit } from '@angular/core';
import {ActivatedRoute} from '@angular/router'; @Component({
selector: 'app-end-page',
templateUrl: './end-page.component.html',
styleUrls: ['./end-page.component.scss']
})
export class EndPageComponent implements OnInit {
constructor(public route:ActivatedRoute) { } ngOnInit() {
console.dir(this.route);
debugger
this.route.params.subscribe((data)=>{
console.log(data); // 打印的是一个对象,key为name和age
});
} }

API js路由跳转

配置动态路由

this.router.navigate(['/newscontent/','1243'])
//this.router.navigate(['/home']);

如果是动态路由,则路由后面的/是不能少的

定义路由

const routes: Routes = [
{ path: 'endpage', component: EndPageComponent }
];
url地址是 http://localhost:4200/endpage/huangbiao/30

获取参数

import { Component, OnInit } from '@angular/core';
import { Router} from '@angular/router'; @Component({
selector: 'app-end-page',
templateUrl: './end-page.component.html',
styleUrls: ['./end-page.component.scss']
})
export class EndPageComponent implements OnInit {
constructor(public router:Router) { } ngOnInit() {
console.dir(this.route);
debugger
this.route.params.subscribe((data)=>{
console.log(data); // 打印的是一个对象
});
} }

get传值

this.router.navigate(['/news'],{
queryParams:{
aid:123
}
});

定义路由

const routes: Routes = [
{ path: 'endpage', component: EndPageComponent }
];
url地址是 http://localhost:4200/endpage/huangbiao/30

获取参数

import { Component, OnInit } from '@angular/core';
import { Router} from '@angular/router'; @Component({
selector: 'app-end-page',
templateUrl: './end-page.component.html',
styleUrls: ['./end-page.component.scss']
})
export class EndPageComponent implements OnInit {
constructor(public router:Router) { } ngOnInit() {
console.dir(this.route);
debugger
this.route.params.subscribe((data)=>{
console.log(data); // 打印的是一个对象
});
} }

  

1.以根路由跳转/login

this.router.navigate(['login']);

2.设置relativeTo相对当前路由跳转,route是ActivatedRoute的实例,使用需要导入ActivatedRoute

this.router.navigate(['login', 1],{relativeTo: route});

3.路由中传参数 /login?name=1

this.router.navigate(['login', 1],{ queryParams: { name: 1 } });

4.preserveQueryParams默认值为false,设为true,保留之前路由中的查询参数/login?name=1 to /home?name=1

this.router.navigate(['home'], { preserveQueryParams: true });

5.路由中锚点跳转 /home#top

this.router.navigate(['home'],{ fragment: 'top' });

6.preserveFragment默认为false,设为true,保留之前路由中的锚点/home#top to /role#top

this.router.navigate(['/role'], { preserveFragment: true });

7.skipLocationChange默认为false,设为true,路由跳转时浏览器中的url会保持不变,但是传入的参数依然有效

this.router.navigate(['/home'], { skipLocationChange: true });

8.replaceUrl默认为true,设为false,路由不会进行跳转

this.router.navigate(['/home'], { replaceUrl: true });

angular路由学习笔记的更多相关文章

  1. Angular快速学习笔记(2) -- 架构

    0. angular 与angular js angular 1.0 google改名为Angular js 新版本的,2.0以上的,继续叫angular,但是除了名字还叫angular,已经是一个全 ...

  2. Angular 快速学习笔记(1) -- 官方示例要点

    创建组件 ng generate component heroes {{ hero.name }} {{}}语法绑定数据 管道pipe 格式化数据 <h2>{{ hero.name | u ...

  3. Angular【学习笔记】

    1.angular入门网站 感谢@菜鸟教程:http://www.runoob.com/angularjs/angularjs-tutorial.html 学习笔记:

  4. Angular 4 学习笔记 从入门到实战 打造在线竞拍网站 基础知识 快速入门 个人感悟

    最近搞到手了一部Angular4的视频教程,这几天正好有时间变学了一下,可以用来做一些前后端分离的网站,也可以直接去打包web app. 环境&版本信息声明 运行ng -v @angular/ ...

  5. angular.js学习笔记--概念总结

    好久没更新了,现在开始学习学习angularjs,并且把学习到的一些知识总结记录一下,方便以后查找以及希望能给初学者一些帮助!(由于本人也是初学ng所以各位慎重理解!) 废话不多说,开始! $root ...

  6. Angular2之路由学习笔记

    目前工作中项目的主要技术栈是Angular2 在这里简单记录一下遇到的问题以及解决方案. 这篇笔记主要记录Angular2 的路由. 官方文档链接:https://angular.cn/docs/ts ...

  7. Angular JS 学习笔记(自定义服务:factory,Promise 模式异步请求查询:$http,过滤器用法filter,指令:directive)

    刚学没多久,作了一个小项目APP,微信企业号开发与微信服务号的开发,使用的是AngularJS开发,目前项目1.0版本已经完结,但是项目纯粹为了赶工,并没有发挥AngularJS的最大作用,这几天项目 ...

  8. Angular.js学习笔记(三)

    一.过滤器 1.uppercase,lowercase 大小写转换{{ "lower cap string" | uppercase }} // 结果:LOWER CAP STRI ...

  9. angular.js学习笔记(二)

    1.安装core ,shared模块 ng g m core  ng g m shared 2.在shared中导入core模块   core模块只加载一次所以将公共组件放到core中 3.创建公共组 ...

随机推荐

  1. ICP备案接入商

    1. 什么是ICP备案中的接入商 ICP备案系统中所说的接入商:是指为您提供虚拟主机.服务器托管或者专线接入的公司. 现在ICP备案的原则是“谁接入谁负责”,接入商一般都有自己的电子平台和工信部对接, ...

  2. JavaWeb:Cookie处理和Session跟踪

    JavaWeb:Cookie处理和Session跟踪 Cookie处理 什么是Cookie Cookie 是存储在客户端计算机上的文本文件,保留了各种跟踪信息.因为HTTP协议是无状态的,即服务器不知 ...

  3. DateAdapterForDay

    public class DateAdapterForDay extends XmlAdapter<String, Date> { private SimpleDateFormat dat ...

  4. python中深复制和浅复制区别

    在python中,对象赋值实际上是对象的的引用,当创建一个对象,然后把它赋值给另外一个变量的时候,python没有拷贝这个对象,而只是拷贝了这个对象的引用,多以就出现了浅复制,即复制后原对象改变后,复 ...

  5. LeetCode 136 Single Number 数组中除一个数外其他数都出现两次,找出只出现一次的数

    Given an array of integers, every element appears twice except for one. Find that single one. class ...

  6. 是时候搞清楚 Spring Boot 的配置文件 application.properties 了!

    在 Spring Boot 中,配置文件有两种不同的格式,一个是 properties ,另一个是 yaml . 虽然 properties 文件比较常见,但是相对于 properties 而言,ya ...

  7. serv-U 7以上版本pasv端口的设置及中文乱码问题

    利用serv-u架设ftp服务器已经是再常见不过了事情了,近日一朋友为图新鲜,弄了个7.4版本的新玩意儿,结果架设上去后,仅开了21端口,用LeapFtp在port模式下连接没问题,但是另一常见的cu ...

  8. Maven的学习资料收集--(九) 构建SSH项目以及专栏maven

    在这里整合一下,使用Maven构建一个SSH项目 1.新建一个Web项目 可以参照前面的博客 2.添加依赖,修改pom.xml <project xmlns="http://maven ...

  9. 阿里云部署安装redis无法访问

    ps:我在linux上安装redis后发现一直端口不通连接不上,折腾一晚上.后来在阿里云官方回复(机器人)中看到下面的回复:       2019/02/28 22:50 自己一试,过完是端口监听是本 ...

  10. 一起来学Spring Cloud | 第一章 :如何搭建一个多模块的springcloud项目

    在spring cloud系列章节中,本来已经写了几个章节了,但是自己看起来有些东西写得比较杂,所以重构了一下springcloud的章节内容,新写了本章节,先教大家在工作中如何搭建一个多模块的spr ...