angular路由学习笔记
文章目录
标签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路由学习笔记的更多相关文章
- Angular快速学习笔记(2) -- 架构
0. angular 与angular js angular 1.0 google改名为Angular js 新版本的,2.0以上的,继续叫angular,但是除了名字还叫angular,已经是一个全 ...
- Angular 快速学习笔记(1) -- 官方示例要点
创建组件 ng generate component heroes {{ hero.name }} {{}}语法绑定数据 管道pipe 格式化数据 <h2>{{ hero.name | u ...
- Angular【学习笔记】
1.angular入门网站 感谢@菜鸟教程:http://www.runoob.com/angularjs/angularjs-tutorial.html 学习笔记:
- Angular 4 学习笔记 从入门到实战 打造在线竞拍网站 基础知识 快速入门 个人感悟
最近搞到手了一部Angular4的视频教程,这几天正好有时间变学了一下,可以用来做一些前后端分离的网站,也可以直接去打包web app. 环境&版本信息声明 运行ng -v @angular/ ...
- angular.js学习笔记--概念总结
好久没更新了,现在开始学习学习angularjs,并且把学习到的一些知识总结记录一下,方便以后查找以及希望能给初学者一些帮助!(由于本人也是初学ng所以各位慎重理解!) 废话不多说,开始! $root ...
- Angular2之路由学习笔记
目前工作中项目的主要技术栈是Angular2 在这里简单记录一下遇到的问题以及解决方案. 这篇笔记主要记录Angular2 的路由. 官方文档链接:https://angular.cn/docs/ts ...
- Angular JS 学习笔记(自定义服务:factory,Promise 模式异步请求查询:$http,过滤器用法filter,指令:directive)
刚学没多久,作了一个小项目APP,微信企业号开发与微信服务号的开发,使用的是AngularJS开发,目前项目1.0版本已经完结,但是项目纯粹为了赶工,并没有发挥AngularJS的最大作用,这几天项目 ...
- Angular.js学习笔记(三)
一.过滤器 1.uppercase,lowercase 大小写转换{{ "lower cap string" | uppercase }} // 结果:LOWER CAP STRI ...
- angular.js学习笔记(二)
1.安装core ,shared模块 ng g m core ng g m shared 2.在shared中导入core模块 core模块只加载一次所以将公共组件放到core中 3.创建公共组 ...
随机推荐
- Java start和run启动线程的区别
我们知道,我们通过调用线程的start方法启动一个线程,那么,我们可以直接调用run方法来启动一个线程吗? 先看下面一段代码: public class Test { public static vo ...
- 时间format函数引爆的知识点和年末有话说
年末感慨 转眼之间,一年的最后一天来了. 2017,技术界貌似正在飞跃.多年的量变终于引起了质变. 人工智能,区块链.对此,我很激动,激动着有点害怕,害怕中有点紧张,还有点渴望.未来的至高点,未来的风 ...
- 使用命令行+代理更新Android SDK
在无桌面的Linux上面安装Jenkins,要配置成Andorid 的持续集成环境Jenkins持续集成Android项目,需要在无桌面的Linux(ubuntu,centos)上安装Android ...
- LeetCode 148 Sort List 链表上的归并排序和快速排序
Sort a linked list in O(n log n) time using constant space complexity. 单链表排序----快排 & 归并排序 (1)归并排 ...
- 002 Add Two Numbers 链表上的两数相加
You are given two non-empty linked lists representing two non-negative integers. The digits are stor ...
- Ubuntu系统下同时打开多个终端窗口的软件
sudo apt-get install Terminator
- HDU 4622 Reincarnation Hash解法详解
今天想学字符串hash是怎么弄的.就看到了这题模板题 http://acm.hdu.edu.cn/showproblem.php?pid=4622 刚开始当然不懂啦,然后就上网搜解法.很多都是什么后缀 ...
- POI 读取Excel数据
private List<LeagueGroup> read() throws IOException{ List<LeagueGroup> leagueGroups=new ...
- android 开发-ListView与ScrollView事件冲突处理(事件分发机制处理)
ListView和ScrollView都存在滚动的效果,所以一般不建议listView和scrollView进行嵌套使用,但有些需求则需要用到两者嵌套.在android的学习中学了一种事件分发处理机制 ...
- Xcode警告忽略
Xcode中 cmd+B 之后总会出现各种各样的警告,对于一个对代码有洁癖的人,这种情况很难忍,接下来我们用些特殊手段强制消除这些警告 一.忽略源文件中的警告 使用编译器宏来操作 #pragma c ...