Angular4のRouterクラスのルーティング例を詳しく解説

零下一度
リリース: 2017-06-26 11:04:40
オリジナル
1535 人が閲覧しました

私は最近 angular4 を勉強しています。以前よりもかなり変わっていて、改善されています。幸いなことに、英語が苦手な方でも、公式ドキュメントやサンプルは中国語で書かれています。大きな違いは学習に役立ちます。

正式アドレス:

学習の過程において、ルーティング(ルーター)機構は切り離せないものであり、多くの場所で使用されます。

最初のルート設定 ルート:

 1 import { NgModule }             from '@angular/core'; 2 import { RouterModule, Routes } from '@angular/router'; 3   4 import { HomeComponent }   from './home.component'; 5 import { LoginComponent }      from './login.component'; 6 import { RegisterComponent }  from './register.component'; 7   8  const routes: Routes = [ 9    { path: '', redirectTo: '/home', pathMatch: 'full' },10    { path: 'home',  component: HomeComponent },11    { path: 'login', component: LoginComponent },12    { path: 'heroes',     component: RegisterComponent }13  ];14  15  @NgModule({16    imports: [ RouterModule.forRoot(routes) ],17    exports: [ RouterModule ]18  })19  export class AppRoutingModule {}
ログイン後にコピー
コードを表示

2番目のルートジャンプ Router.navigate

1 navigate(commands: any[], extras?: NavigationExtras) : Promise<boolean>
ログイン後にコピー
 1 interface NavigationExtras { 2     relativeTo : ActivatedRoute 3     queryParams : Params 4     fragment : string 5     preserveQueryParams : boolean 6     queryParamsHandling : QueryParamsHandling 7     preserveFragment : boolean 8     skipLocationChange : boolean 9     replaceUrl : boolean10 }
ログイン後にコピー
コードを表示1. root ルート /login でジャンプします。

this.router.navigate(['login']);

2. 現在のルートを基準にしてジャンプするように相対的に設定します。これを使用するには、ActivatedRoute

をインポートする必要があります。 this.router.navigate(['login', 1],{relativeTo:route});

3. パラメータ/login?name=1 を渡しますthis.router.navigate(['login', 1],{relativeTo: route}); 

3.路由中传参数 /login?name=1

this.router.navigate(['login', 1],{ queryParams: { name: 1 } }); 

4.preserveQueryParams默认值为false,设为true,保留之前路由中的查询参数/login?name=1 to /home?name=1

this.router.navigate(['home'], { preserveQueryParams: true }); 

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 });

this.router.navigate([' login', 1 ],{ queryParams: { name: 1 } });

4.preserveQueryParams のデフォルト値は false で、true に設定され、クエリ パラメータ /login?name=1 から /home?name が保持されます。 = 前のルートの 1

this.router.navigate(['home'], {preserveQueryParams: true });

5. ルート内のアンカー ポイントは /home#top にジャンプします。 🎜🎜 this.router.navigate (['home'],{ flagment: 'top' });🎜🎜6.preserveFragment のデフォルトは false、true に設定され、アンカー ポイント /home#top を /role#top に保持します。前のルートで🎜🎜 this.router.navigate(['/role'], {preserveFragment: true }); 🎜🎜7.skipLocationChange のデフォルトは false、true に設定、ルートがジャンプしてもブラウザは変更されませんが、渡されたパラメータは引き続き有効です🎜🎜this.router.navigate(['/home'], { stopLocationChange: true });🎜🎜8.replaceUrl が設定されている場合、デフォルトは true になりますfalse にすると、ルートはジャンプしません🎜 🎜this.router.navigate(['/home'], { replaceUrl: true }); 🎜🎜 🎜🎜まだまだ学ぶべきことがたくさんあります。まずジャンプについてここに書きます。みんながお互いから学び、経験した落とし穴を共有できることを願っています。 🎜🎜🎜

以上がAngular4のRouterクラスのルーティング例を詳しく解説の詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。

関連ラベル:
ソース:php.cn
このウェブサイトの声明
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。
人気のチュートリアル
詳細>
最新のダウンロード
詳細>
ウェブエフェクト
公式サイト
サイト素材
フロントエンドテンプレート
私たちについて 免責事項 Sitemap
PHP中国語ウェブサイト:福祉オンライン PHP トレーニング,PHP 学習者の迅速な成長を支援します!