Angular路由参数传递
一、路由时传递参数的方式
1、在查询参数中传递数据
//页面
<a routerLink="/product" [queryParams]="{id:1}">商品详情</a>
//ts获取参数
export class ProductComponent implements OnInit {
private productId: number;
constructor(private routeInfo: ActivatedRoute) { }
ngOnInit() {
this.productId = this.routeInfo.snapshot.queryParams['id'];
}
}
相应的后台获取是:ActivedRoute.queryParams[id]
2、在路由路径中传递数据
//页面
<a [routerLink]="['product', 1]">商品详情</a>
//后台页面,先修改路由定义,app-routing.modules.ts中
const routes: Routes = [
{path: 'product/:id', component: ProductComponent},
{path: '**', component: HomeComponent},
]; this.productId = this.routeInfo.snapshot.params['id'];
在路由定义时,定义为:product/:id,其中“:id”代表参数
3、在路由配置中传递数据
//页面
<input type="button" value="商品详情" (click)='toProductDetails()' >
页面跳转:
constructor(
private router: Router, //这里需要注入Router模块
){} toProductDetails(){
//这是在html中绑定的click跳转事件
this.router.navigate(['product-detail'], {
queryParams: {
productId: '1',
title: 'moon'
}
});
}
接收参数:
constructor(
private activatedRoute: ActivatedRoute, //这里需要注入ActivatedRoute模块
) {
activatedRoute.queryParams.subscribe(queryParams => {
let productId = queryParams.productId;
let title = queryParams.title;
});
}
二、后台接收路由参数方式
1、snapshot和subscribe两种,区别在于在路由地址不变的情况下,若参数发生变化,后者所接收的参数也会随之变化,前者不变。
三、路由重定向
访问一个特定的地址时,会将其重定向到另一个指定的地址
1 //在定义路由时
2 {path: '', redirectTo: '/home', pathMatch: 'full' },
3 {path: 'home', component : HomeComponent},
参考:
angular4.0中路由传递参数、获取参数最nice的写法
Angular路由参数传递的更多相关文章
- angular路由——ui.route
angular路由 使用案例 <!DOCTYPE html> <html lang="en"> <head> <meta charset= ...
- angular路由详解:
1.$routeProvider ngRoute模块中的服务 2.otherwise:设置用于路由改变时,与任何其他定义的路由无法匹配的时候执行的代码 3.when:为$route服务定义新的路由 例 ...
- angular 路由的引用
使用angular路由 遇到的坑. 使用cmd 安装好node.js 安装成功 输入node -v 能查看版本说明安装成功 搭建angular项目输入命令 npm install -g angu ...
- angular 路由项目例子
angular 路由是我在工作中体验非常便捷的一点, 这是详细的API ,查看API 可以了解很多东西, https://github.com/angular-ui/ui-router/wiki/Qu ...
- angular路由(自带路由篇)
一.angular路由是什么? 为了实现SPA多视图的切换的效果,其原理可简述为每个 URL 都有对应的视图和控制器.所以当我们给url后面拼上不同的参数就能通过路由实现不同视图的切换. 二.文件总览 ...
- Angular路由守卫 canActivate
作用 canActivate 控制是否允许进入路由. canActivateChild 等同 canActivate,只不过针对是所有子路由. 关键代码 创建路由守卫 import { Injecta ...
- Angular路由守卫 canDeactivate
目的 离开页面时,做出逻辑判断 以ng-alain的项目为基础做演示 效果如图: 关键代码 定义一个CanDeactivateGuardService export class CanDeactiva ...
- Angular 路由守卫
1. 路由 Angular路由: 可以控制页面跳转:可以在多视图间切换: 2. 路由守卫 Angular路由守卫: 在进入或离开某路由时,用于判断是否可以离开.进入某路由::: return true ...
- angularjs中使用锚点,angular路由导致锚点失效的两种解决方案
壹 ❀ 引 公司新项目开发中,首页要做个楼层导航效果(如下图),要求能点击图标对应跳到楼层即可,因为不需要跳转过度动画,也要求最好别用JQ,想着原生js操作dom计算top的兼容性,想着用锚点实现算 ...
随机推荐
- linux 查找文件并移动
find . -name '10-*.dat' -exec mv {} ../ \; 这里: => -exec mv {} /mnt/mp3 \; - 运行mv命令. => {} ...
- Centos6.5 --配置 vsftp server
事实上我这么懒得人是不想配置什么ftpserver的(毕竟动起来都认为麻烦).可是因为本菜鸟才刚開始步入linux的大坑.尽管有装Centos7的真机,可是因为一时还是脱离不了Windows平台,所以 ...
- HDU 1075 What Are You Talking About (Trie)
What Are You Talking About Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 102400/204800 K ...
- hbase练习题
-- 配置环境变量,因为在hbase中有的地方可能用到了环境变量-- bin/start-hbase.sh-- bin/hbase shell-- 访问http://mini0:16010/ 可以看浏 ...
- 在linux下安装配置rabbitMQ详细教程
在linux下安装配置rabbitMQ详细教程 2017年12月20日 17:34:47 阅读数:7539 安装Erlang 由于RabbitMQ依赖Erlang, 所以需要先安装Erlang. Er ...
- Asp.Net中使用水晶报表
Asp.Net中使用水晶报表(上) 在我们对VS.Net中的水晶报表(Crystal Reports)进行研究之前,我和我朋友对如何将这个复杂的东东加入我们的Web应用有着非常的好奇心.一周以后,在阅 ...
- 命令行执行php
D:\software\phpStudy\php55
- .NET中二进制图片的存储与读取
判断HttpContext是否为空: string configPath = "img/defaultPhoto.png"; if (HttpContext.Current != ...
- 【POJ】2942 Knights of the Round Table(双连通分量)
http://poj.org/problem?id=2942 各种逗.... 翻译白书上有:看了白书和网上的标程,学习了..orz. 双连通分量就是先找出割点,然后用个栈在找出割点前维护子树,最后如果 ...
- 终于找到了最新的Chemdarw注册码
随着中国人对知识产权的保护意识提升,正版软件越来越流行,只有一小部分人还在寻找Chemdarw破解版.最新的ChemDraw 15正式版本已经强势来袭,在获取软件安装包之后需要有效的注册码才能激活软件 ...