Angular路由RouterLink指令和Router

 

 

RouterLink指令

RouterLink的属性

[queryParams](查询参数)

用法:

<a [routerLink]="['/danone']" [queryParams]="{id: 1, name: 'abc', date: '2020-6-19'}" [state]="{tracingId: 123}">达能</a> 

 

url显示内容:http://localhost:4200/danone?id=1&name=abc&date=2020-6-19

 

[fragment] (片段)

跟在#后面的一个片段

用法:

<a [routerLink]="['/danone']" fragment="aaa">达能</a>

 

url显示内容:http://localhost:4200/danone#aaa

 

[queryParamsHandling](查询参数处理)

用法:

<a [routerLink]="['/danone']" [queryParams]="{test: 'b2'}" queryParamsHandling="merge">queryParamsHandling</a> 

 

queryParamsHandling的值

  • 默认为空''——表示只使用当前查询参数;
  • merge——把老的查询参数合并进新的查询参数中 ;
  • preserve——保持当前的查询参数;

 

[preserveFragment](保留片段)

保留上一个url中的#号后面的片段

用法:

<a [routerLink]="['/bifidus/', 2]" preserveFragment>保留片段</a>

 

上一个url: http://localhost:4200/nestle?session_id=123#anchor

当前url: http://localhost:4200/bifidus/2#anchor

 

[skipLocationChange]

默认true——路由跳转时浏览器中的url保持不变,但是传入的参数依然有效

false——与true相反

用法:

<a [routerLink]="['/bifidus/', 2]" skipLocationChange>跳过位置变化</a>

 

url不变,内容正常改变

 

[replaceUrl] ???

true 路由跳转

false 路由不调整

 

[state]

用法:

<a [routerLink]="['/tonze']" [state]="{tracingId: 123}">state</a>

 

url: http://localhost:4200/tonze

相应页面component ts

复制代码
import { Component, OnInit } from '@angular/core';
import { ActivatedRoute, Router, RouterEvent, NavigationStart } from '@angular/router';
import { filter } from 'rxjs/operators';

@Component({
  selector: 'app-tonze',
  templateUrl: './tonze.component.html',
  styleUrls: ['./tonze.component.css']
})
export class TonzeComponent implements OnInit {

  constructor(public route: ActivatedRoute, public router:Router) { }

  ngOnInit() {
    this.router.events.pipe(filter(e => e instanceof NavigationStart)).subscribe(e => {
      const navigation = this.router.getCurrentNavigation();
      console.log(navigation.extras.state.tracingId);
    })
  }

}
复制代码

 

 

RouterLink指令总是把新提供的输入看作是对当前URL的增量修改。

 

 

Router

一个提供导航和操纵URL能力的NgModule

 

posted on   独自去流浪  阅读(41)  评论(0编辑  收藏  举报

编辑推荐:
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
阅读排行:
· 终于写完轮子一部分:tcp代理 了,记录一下
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
历史上的今天:
2020-08-30 Angular中的可观察对象
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

统计

点击右上角即可分享
微信分享提示