angular 路由跳转以及传参的几种方式

摘要:
productId=1&title=moon)这种方式配置路由,其中:id是必需的参数,其它的是可配的,写在?后面:{path:'second/:id',component:SecondComponent},通过ts代码跳转:this.router.navigate;获取参数的方式:constructor{}varid=this.route.snapshot.paramMap.get;在comoent中需要这么获取‘?’后面的参数,将参数写在queryParams里this.router.navigate;通过ActivatedRoute来获取constructor{}ngOnInit(){varid=this.route.snapshot.paramMap.get;this.name=id;console.log;this.route.queryParams.subscribe;}1.this.router.navigate;以根路由为起点跳转2.this.router.navigate;默认值为根路由,设置后相对当前路由跳转,route是ActivatedRoute的实例,使用需要导入ActivatedRoute3.this.router.navigate;路由中传参数/user/1?id=14.this.router.navigate;默认值为false,设为true,保留之前路由中的查询参数/user?--数组格式传参,注是第二种试的routerLink是用[]包括--˃点击查看文章详情

通过域名跳转的方式获取参数(http://localhost:4200/second/110?productId=1&title=moon)

这种方式配置路由,其中:id是必需的参数,其它的是可配的,写在?后面:

{ path: 'second/:id', component: SecondComponent },
通过ts代码跳转:
this.router.navigate(['/second',110], { queryParams: { productId: '1', title: 'moon' } });

获取【:id】参数的方式:

constructor(private route: ActivatedRoute, private router: Router) { }
var id = this.route.snapshot.paramMap.get('id');
在comoent中需要这么获取‘?’ 后面的参数,将参数写在queryParams里
this.router.navigate(['/second',110], { queryParams: { productId: '1', title: 'moon' } });
通过ActivatedRoute来获取
  constructor(private route: ActivatedRoute, private router: Router) { }

  ngOnInit() {

    var id = this.route.snapshot.paramMap.get('id');
    this.name = id;
    console.log(id);
    
    this.route.queryParams.subscribe(param => {
      console.log(param);
      console.log(param.title);
      console.log(param['title']);
      console.log(param.productId);
      console.log(param['productId']);
    });
  }

1.this.router.navigate(['user', 1]);
以根路由为起点跳转

2.this.router.navigate(['user', 1],{relativeTo: route});
默认值为根路由,设置后相对当前路由跳转,route是ActivatedRoute的实例,使用需要导入ActivatedRoute

3.this.router.navigate(['user', 1],{ queryParams: { id: 1 } });
路由中传参数 /user/1?id=1

4.this.router.navigate(['view', 1], { preserveQueryParams: true });
默认值为false,设为true,保留之前路由中的查询参数/user?id=1 to /view?id=1

5.this.router.navigate(['user', 1],{ fragment: 'top' });
路由中锚点跳转 /user/1#top

6.this.router.navigate(['/view'], { preserveFragment: true });
默认值为false,设为true,保留之前路由中的锚点/user/1#top to /view#top

7.this.router.navigate(['/user',1], { skipLocationChange: true });
默认值为false,设为true路由跳转时浏览器中的url是跳转前的路径,但是传入的参数依然有效

8.this.router.navigate(['/user',1], { replaceUrl: true });
未设置时默认为true,设置为false路由不会进行跳转

9.页面里url路转写法

<nav>
    <a routerLink="/heroes/{{id}}">点击查看文章详情</a>
    <!--数组格式传参,注是第二种试的routerLink是用[]包括-->
    <a [routerLink]="['/heroes', num]">点击查看文章详情</a>
</nav>

免责声明:文章转载自《angular 路由跳转以及传参的几种方式》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇细说 Form (表单)JS无限添加HTML到指定位置下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

WPF转换器之通用转换器

WPF中的转换器是一个非常好的数据类型转换解决方案,实用和强大, 它的作用是将源数据转换为WPF自身需要的类型,对数据实体没有侵略性,会在项目工程中频繁使用。所以掌握转换器是WPF开发的必备技能。 我刚接触转换器的时候,没有考虑通用性,每次遇到一个转换需求都会去创建一个新的转换器,久而久之,项目中的转换器已多如牛毛 这当然是我无法容忍的,我决定用一种通用...

【转】inputfile的本地图片预览+等比例缩放兼容IE8、火狐

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" ""><html xmlns=""><head><meta http-equiv="Content-Type" content="text/html; charset=gb2312" /&...

Angular 报错 Can't bind to 'formGroup' since it isn't a known property of 'form'

错误描述 当form表单加FormGroup属性时报错 Can't bind to 'formGroup' since it isn't a known property of 'form' <form nz-form [formGroup]="valForm" (ngSubmit)="submit()" role="form">...

Linux 路由 策略路由

Linux 路由 策略路由 目录 Linux 路由 策略路由 一、路由表编辑路由表配置文件:/etc/iproute2/rt_tables添加删除修改路由表 二、IP策略 查看IP策略 添加IP策略规则 根据源地址决定路由表 根据目的地址决定路由表 根据网卡设备决定路由表 此外还可以根据其他条件进行设置,例如tos等等 删除IP策略规则...

openwrt-智能路由器hack技术(2)---"网路信息监控和窃取"

openwrt-智能路由器hack技术(2)---"网路信息监控和窃取" 1   导读 PS:之前写的一个文章,现在发现结构内容排版不是太好,导致阅读体验太差,影响传播和SEO,所以现在整理拆分一下,从小处写起, 本系列介绍了两个关于智能路由器的hack技术,供大家赏玩一下。 本文主要介绍openwrt-智能路由器的数据监控功能。(为了维持本文一定的独立...

打造自己的数据访问层(一)

项目开发中,大多数开发人员的核心工作就是如何对数据进行存储及访问。为了进行数据操作,我们首先得解决几个基本问题:1、如何与一个数据库建立连接。2、如何从数据库读取相应的数据。3、如何对数据表进行增改操作。.NET中,ADO.NET很方便的解决了上面三个问题,我们可以使用DbConnection进行连接,DataSet进行数据存储,DataAdapter进行...