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
标签:RouterLink,http,url,Router,4200,Angular,localhost From: https://www.cnblogs.com/cathy1024/p/13166532.html