本文主要介紹的是angular2中router路由跳轉(zhuǎn)navigate的使用與刷新頁面問題的相關(guān)內(nèi)容,分享出供大家參考學(xué)習(xí),下面來看看詳細(xì)的介紹:
一、router.navigate的使用
navigate是Router類的一個(gè)方法,主要用來跳轉(zhuǎn)路由。
函數(shù)定義:
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]);
以根路由為起點(diǎn)跳轉(zhuǎn)
2.this.router.navigate(['user', 1],{relativeTo: route});
默認(rèn)值為根路由,設(shè)置后相對(duì)當(dāng)前路由跳轉(zhuǎn),route是ActivatedRoute的實(shí)例,使用需要導(dǎo)入ActivatedRoute
3.this.router.navigate(['user', 1],{ queryParams: { id: 1 } });
路由中傳參數(shù) /user/1?id=1
4.this.router.navigate(['view', 1], { preserveQueryParams: true });
默認(rèn)值為false,設(shè)為true,保留之前路由中的查詢參數(shù)/user?id=1 to /view?id=1
5.this.router.navigate(['user', 1],{ fragment: 'top' });
路由中錨點(diǎn)跳轉(zhuǎn) /user/1#top
6.this.router.navigate(['/view'], { preserveFragment: true });
默認(rèn)值為false,設(shè)為true,保留之前路由中的錨點(diǎn)/user/1#top to /view#top
7.this.router.navigate(['/user',1], { skipLocationChange: true });
默認(rèn)值為false,設(shè)為true路由跳轉(zhuǎn)時(shí)瀏覽器中的url會(huì)保持不變,但是傳入的參數(shù)依然有效
8.this.router.navigate(['/user',1], { replaceUrl: true });
未設(shè)置時(shí)默認(rèn)為true,設(shè)置為false路由不會(huì)進(jìn)行跳轉(zhuǎn)
二、router.navigate刷新頁面問題
造成這個(gè)問題一般是因?yàn)槲覀冊(cè)?lt;form>表單中使用<button>時(shí)忘記添加type屬性,在表單中,如果忘記給按鈕添加屬性,會(huì)默認(rèn)為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>
總結(jié)
以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對(duì)大家學(xué)習(xí)或者使用Angular.js能帶來一定的幫助,如果有疑問大家可以留言交流,謝謝大家對(duì)錯(cuò)新站長站的支持。
更多關(guān)于AngularJS相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《AngularJS指令操作技巧總結(jié)》、《AngularJS入門與進(jìn)階教程》及《AngularJS MVC架構(gòu)總結(jié)》
新聞熱點(diǎn)
疑難解答
圖片精選