10 Angular 中的路由

10.1 創(chuàng)建靜態(tài)路由

1)找到 app-routing.module.ts 配置路由
2)引入組件

import { HomeComponent } from './home/home.component'; 
import { NewsComponent } from './news/news.component';
 import { NewscontentComponent } from './newscontent/newscontent.component';

3)配置路由

const routes: Routes = [ 
    {path: 'home', component: HomeComponent}, 
    {path: 'news', component: NewsComponent}, 
    {path: 'newscontent/:id', component: NewscontentComponent}, 
    { path: '**', redirectTo: '/home'} 
];

4)找到 app.component.html 根組件模板,配置 router-outlet 顯示動態(tài)加載的路由

<h1>
 <a routerLink="/home">首頁</a> 
<a routerLink="/news">新聞</a> 
</h1>
<router-outlet></router-outlet>

10.2 Angular routerLink 跳轉(zhuǎn)頁面 默認路 由

<a routerLink="/home">首頁</a> <a routerLink="/news">新聞</a>
//匹配不到路由的時候加載的組件 或者跳轉(zhuǎn)的路由 
{ 
path: '**', /*任意的路由*/ 
// component:HomeComponent 
redirectTo:'home' 
}

10.3 Angular routerLinkActive 設(shè) 置 routerLink 默認選中路由

<h1> 
    <a routerLink="/home" routerLinkActive="active">首頁</a> 
    <a routerLink="/news" routerLinkActive="active">新聞</a> 
</h1>
<h1> 
    <a [routerLink]="[ '/home' ]" routerLinkActive="active">首頁</a> 
    <a [routerLink]="[ '/news' ]" routerLinkActive="active">新聞</a> 
</h1>
.active{ color:red; }

10.4 靜態(tài)路由傳值

Get傳值
1、跳轉(zhuǎn)

  <a [routerLink]="[ '/newscontent']" [queryParams]="{aid:key}">{{ key }} --- {{ item }}</a>

2、接收

  import { ActivatedRoute } from '@angular/router';
  constructor(public route:ActivatedRoute) { }

  ngOnInit() {
    // console.log(this.route)
    this.route.queryParams.subscribe((data)=>{
      console.log(data)
    })
  }

10.5 動態(tài)路由

1.配置動態(tài)路由

const routes: Routes = [ 
    {path: 'home', component: HomeComponent}, 
    {path: 'news', component: NewsComponent}, 
    {path: 'newscontent/:id', component: NewscontentComponent}, 
    { path: '', redirectTo: '/home', pathMatch: 'full' }
];

2.跳轉(zhuǎn)傳值

<a [routerLink]="[ '/newscontent/',aid]">跳轉(zhuǎn)到詳情</a> 
<a routerLink="/newscontent/{{aid}}">跳轉(zhuǎn)到詳情</a>

3.獲取動態(tài)路由的值

import { ActivatedRoute} from '@angular/router';
    constructor( private route: ActivatedRoute) {
}
ngOnInit() { 
    console.log(this.route.params); 
    this.route.params.subscribe(data=>this.id=data.id); 
}

10.6 動態(tài)路由的JS 跳轉(zhuǎn)

  1. 引入
import { Router } from '@angular/router';

2.初始化

export class HomeComponent implements OnInit { constructor(private router: Router) {
}
    ngOnInit() { }
    goNews(){ 
        // this.router.navigate(['/news', hero.id]);
        this.router.navigate(['/news']);//靜態(tài)
    }
}

3.路由跳轉(zhuǎn)

this.router.navigate(['/news', hero.id]); //動態(tài)

10.7 路由 get 傳值 js 跳轉(zhuǎn)

  1. 引入 NavigationExtras
import { Router ,NavigationExtras} from '@angular/router';

2.定義一個 goNewsContent 方法執(zhí)行跳轉(zhuǎn),用 NavigationExtras 配置傳參。

goNewsContent(){
    let navigationExtras: NavigationExtras = { 
        queryParams: { 'session_id': '123' }, 
        fragment: 'anchor' 
    };
this.router.navigate(['/news'],navigationExtras);
}

3.獲取 get 傳值

constructor(private route: ActivatedRoute) { console.log(this.route.queryParams); }

10.8 父子路由

  1. 創(chuàng)建組件引入組件
import { NewsaddComponent } from './components/newsadd/newsadd.component'; 
import { NewslistComponent } from './components/newslist/newslist.component';
  1. 配置路由
{
      path: 'news', component:NewsComponent,
      children: [
            { path:'newslist',component:NewslistComponent }, 
            { path:'newsadd', component:NewsaddComponent},
            {path:'**', redirectTo: 'newslist'}
      ]
}
  1. 父組件中定義router-outlet
    想在父組件哪里用就在哪里調(diào)用
<router-outlet></router-outlet>
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關(guān)閱讀更多精彩內(nèi)容

  • 一、SPA的概念 首先明確一個概念,SPA,全稱為Single Page Application單頁應用,一個單頁...
    耦耦閱讀 6,069評論 0 3
  • 路由要解決的核心問題是通過建立URL和頁面的對應關(guān)系,使得不同的頁面可以用不同的URL表示。在angular中,頁...
    oWSQo閱讀 1,372評論 0 1
  • 因工作需要學習angular ,特此總結(jié)一下~ 一、angular安裝 1、安裝前準備工作:1.1、安裝 node...
    johnnie_wang閱讀 952評論 1 3
  • 摘要:在本教程中,Ahmed Bouchefra 介紹了angular路由器(router),以及如何使用它創(chuàng)建客...
    哈維爾23456閱讀 3,414評論 0 3
  • 忘憂怡情百花中, 香染雪霞煩惱空。 春露沾濡芳草葉, 夏花楚楚少女風。 博雅?李雪霞 忘憂草花語——偉大的母親,忘...
    姜冠閱讀 241評論 0 2

友情鏈接更多精彩內(nèi)容