本文主要介紹的是angular2中router路由跳轉navigate的使用與刷新頁面問題的相關內容,分享出供大家參考學習,下面來看看詳細的介紹:
一、router.navigate的使用
navigate是Router類的一個方法,主要用來跳轉路由。
函數定義:
navigate(commands: any[], extras?: NavigationExtras) : Promise`<boolean>`
interface NavigationExtras { relativeTo : ActivatedRoute queryParams : Params fragment : string preserveQueryParams : boolean preserveFragment : boolean skipLocationChange : boolean replaceUrl : boolean}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路由不會進行跳轉
二、router.navigate刷新頁面問題
造成這個問題一般是因為我們在<form>表單中使用<button>時忘記添加type屬性,在表單中,如果忘記給按鈕添加屬性,會默認為submit
<button (click)="toDetail()">detail</button>
toDetail() { this._router.navigate(['/detail']);}解決方法:
1.添加type
<button type="button" (click)="toDetail()">detail</button>
2.click添加false
<button (click)="toDetail();false">detail</button>
總結
以上就是這篇文章的全部內容了,希望本文的內容對大家學習或者使用Angular.js能帶來一定的幫助,如果有疑問大家可以留言交流,謝謝大家對武林網的支持。
更多關于AngularJS相關內容感興趣的讀者可查看本站專題:《AngularJS指令操作技巧總結》、《AngularJS入門與進階教程》及《AngularJS MVC架構總結》
新聞熱點
疑難解答