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上安装Nginx及常用命令
一.Linux安装软件常用方法 1.rpm(或pkg)安装,类似于Windows安装程序,是预编译好的程序. 1)使用的是通用参数编译,配置参数不是最佳 2)可控制性不强,比如对程序特定组件的定制性安 ...
- windows 短文件名/短路径名规则
How Windows Generates 8.3 File Names from Long File Names Windows generates short file names from lo ...
- 第二百二十六节,jQuery EasyUI,Tree(树)组件
jQuery EasyUI,Tree(树)组件 本节课重点了解 EasyUI 中 Tree(树)组件的使用方法,这个组件依赖于 Draggable(拖 动)和 Droppable(放置)组件. 一.加 ...
- 【BZOJ】1632: [Usaco2007 Feb]Lilypad Pond(bfs)
http://www.lydsy.com/JudgeOnline/problem.php?id=1632 我简直是个sb... ... bfs都不会写.. 算方案还用2个bfs! 都不会整合到一个! ...
- Openstack(Kilo)安装系列之neutron(九)
控制节点 Before you configure the OpenStack Networking (neutron) service, you must create a database, se ...
- Bootstrap的下拉菜单float问题
在学习bootstrap中的下拉菜单时,遇到下面情况: <div class="dropdown"> <button class="btn btn-de ...
- CentOS7.0 安装 Nginx
记录下,方便以后查阅. 1.安装依赖库 yum install gcc-c++ yum install pcre pcre-devel yum install zlib zlib-devel yum ...
- 【BZOJ4557】[JLoi2016]侦察守卫 树形DP
[BZOJ4557][JLoi2016]侦察守卫 Description 小R和B神正在玩一款游戏.这款游戏的地图由N个点和N-1条无向边组成,每条无向边连接两个点,且地图是连通的.换句话说,游戏的地 ...
- java的奇技淫巧--意外行为与特性(译文)
Java是一种非常成熟的编程语言 - 事实上,它已经走过21年了,如果它是一个人,它可以在美国随便混!随着年龄的增长,智慧也在增长,而至少有时候,有些东西会变得很怪异.在本文中,我将介绍Java语言的 ...
- duboo服务调用不到的原因(dubbo启动消费者报错:No provider available for the service)
com.alibaba.dubbo.rpc.RpcException: Failed to invoke the method queryTemplate in the service com.x.a ...