// queryParams 傳遞的是一個對象 <a [routerLink]="[ '/endpage/']" [queryParams]="{name:'huangbiao',age:30}">endpage</a>
const routes: Routes = [ { path: 'endpage', component: EndPageComponent } ];
url的地址是 http://localhost:4200/endpage?name=huangbiao&age=30
import { Component, OnInit } from '@angular/core'; import {ActivatedRoute} from '@angular/router'; @Component({ selector: 'app-end-page', templateUrl: './end-page.component.html', styleUrls: ['./end-page.component.scss'] }) export class EndPageComponent implements OnInit { constructor(public route:ActivatedRoute) { } ngOnInit() { console.dir(this.route); debugger this.route.params.subscribe((data)=>{ console.log(data); // 打印的是一個對象,key爲name和age }); } }
獲取參數要依賴注入 route:ActivatedRoute
對象css
// 第一個參數對應路由的name,第二個參數對應路由的age <a [routerLink]="[ '/endpage/', 'huangbiao','30' ]">endpage</a>
const routes: Routes = [ { path: 'endpage/:name/:age', component: EndPageComponent } ];
url地址是 http://localhost:4200/endpage/huangbiao/30
import { Component, OnInit } from '@angular/core'; import {ActivatedRoute} from '@angular/router'; @Component({ selector: 'app-end-page', templateUrl: './end-page.component.html', styleUrls: ['./end-page.component.scss'] }) export class EndPageComponent implements OnInit { constructor(public route:ActivatedRoute) { } ngOnInit() { console.dir(this.route); debugger this.route.params.subscribe((data)=>{ console.log(data); // 打印的是一個對象,key爲name和age }); } }
this.router.navigate(['/newscontent/','1243']) //this.router.navigate(['/home']);
若是是動態路由,則路由後面的/
是不能少的html
const routes: Routes = [ { path: 'endpage', component: EndPageComponent } ];
url地址是 http://localhost:4200/endpage/huangbiao/30
import { Component, OnInit } from '@angular/core'; import { Router} from '@angular/router'; @Component({ selector: 'app-end-page', templateUrl: './end-page.component.html', styleUrls: ['./end-page.component.scss'] }) export class EndPageComponent implements OnInit { constructor(public router:Router) { } ngOnInit() { console.dir(this.route); debugger this.route.params.subscribe((data)=>{ console.log(data); // 打印的是一個對象 }); } }
this.router.navigate(['/news'],{ queryParams:{ aid:123 } });
const routes: Routes = [ { path: 'endpage', component: EndPageComponent } ];
url地址是 http://localhost:4200/endpage/huangbiao/30
import { Component, OnInit } from '@angular/core'; import { Router} from '@angular/router'; @Component({ selector: 'app-end-page', templateUrl: './end-page.component.html', styleUrls: ['./end-page.component.scss'] }) export class EndPageComponent implements OnInit { constructor(public router:Router) { } ngOnInit() { console.dir(this.route); debugger this.route.params.subscribe((data)=>{ console.log(data); // 打印的是一個對象 }); } }
1.以根路由跳轉/login瀏覽器
this.router.navigate(['login']);app
2.設置relativeTo相對當前路由跳轉,route是ActivatedRoute的實例,使用須要導入ActivatedRoutethis
this.router.navigate(['login', 1],{relativeTo: route});url
3.路由中傳參數 /login?name=1spa
this.router.navigate(['login', 1],{ queryParams: { name: 1 } });.net
4.preserveQueryParams默認值爲false,設爲true,保留以前路由中的查詢參數/login?name=1 to /home?name=1debug
this.router.navigate(['home'], { preserveQueryParams: true });code
5.路由中錨點跳轉 /home#top
this.router.navigate(['home'],{ fragment: 'top' });
6.preserveFragment默認爲false,設爲true,保留以前路由中的錨點/home#top to /role#top
this.router.navigate(['/role'], { preserveFragment: true });
7.skipLocationChange默認爲false,設爲true,路由跳轉時瀏覽器中的url會保持不變,可是傳入的參數依然有效
this.router.navigate(['/home'], { skipLocationChange: true });
8.replaceUrl默認爲true,設爲false,路由不會進行跳轉
this.router.navigate(['/home'], { replaceUrl: true });