angular 路由

命令創(chuàng)建項(xiàng)目
  1. 命令創(chuàng)建項(xiàng)目
ng new angualrdemo08 --skip-install
圖片.png
  1. 創(chuàng)建需要的組件
ng g component home 
ng g component news 
ng g component newscontent
  1. 找到 app-routing.module.ts 配置路由
  • 引入組件
import { HomeComponent } from './home/home.component';
import { NewsComponent } from './news/news.component'; 
import { NewscontentComponent } from'./newscontent/newscontent.component';
  • 配置路由
const routes: Routes = [ 
{path: 'home', component: HomeComponent}, 
{path: 'news', component: NewsComponent}, 
{path: 'newscontent/:id', component: NewscontentComponent}, 
{ path: '', redirectTo: '/home', pathMatch: 'full' }
 ];
  1. 找到 app.component.html 根組件模板,配置 router-outlet 顯示動(dòng)態(tài)加載的路由
<h1><a routerLink="/home">首頁(yè)</a> <a routerLink="/news">新聞</a> </h1>
 <router-outlet></router-outlet>

Angular routerLink 跳轉(zhuǎn)頁(yè)面 默認(rèn)路由

//匹配不到路由的時(shí)候加載的組件 或者跳轉(zhuǎn)的路由 
{ path: '**', /*任意的路由*/ // 
component:HomeComponent,
redirectTo:'home' 
}

設(shè)置選中樣式

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

動(dòng)態(tài)路由

配置動(dòng)態(tài)路由

const routes: Routes = [ 
{path: 'home', component: HomeComponent}, 
{path: 'news', component: NewsComponent}, 
{path: 'newscontent/:id', component: NewscontentComponent}, 
{ path: '', redirectTo: '/home', pathMatch: 'full' } 
];
//跳轉(zhuǎn)傳值
<a [routerLink]="[ '/newscontent/',aid]">跳轉(zhuǎn)到詳情</a> 
<a routerLink="/newscontent/{{aid}}">跳轉(zhuǎn)到詳情</a>

獲取動(dòng)態(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); 
}

動(dòng)態(tài)路由的 js 跳轉(zhuǎn)

import { Router } from '@angular/router';
export class HomeComponent implements OnInit { 
constructor(private router: Router) { } 
ngOnInit() { }
goNews(){ 
// this.router.navigate(['/news', hero.id]); 
this.router.navigate(['/news']); 
} }

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

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

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

goNewsContent(){ 
let navigationExtras: NavigationExtras = { 
queryParams: { 'session_id': '123' }, 
fragment: 'anchor' 
};
this.router.navigate(['/news'],navigationExtras); }
//獲取 get 傳值
constructor(private route: ActivatedRoute) { console.log(this.route.queryParams); 
}

父子路由

{ 
path: 'news', 
component:NewsComponent, 
children: [ 
{ path:'newslist', component:NewslistComponent },
{ path:'newsadd', component:NewsaddComponent }
 ] 
}
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

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

  • 摘要:在本教程中,Ahmed Bouchefra 介紹了angular路由器(router),以及如何使用它創(chuàng)建客...
    哈維爾23456閱讀 3,417評(píng)論 0 3
  • 一:路由基礎(chǔ) 什么是路由: 在web開(kāi)發(fā)中,路由的概念由來(lái)已久,簡(jiǎn)而言之,就是利用URL的唯一性來(lái)指定特定的事物,...
    真的稻城閱讀 6,069評(píng)論 2 7
  • 傾家蕩產(chǎn)買入文乃股?。?!文乃天外飛仙,唯我文乃(破嗓)!! 任你們前十二集蹦來(lái)蹦去,我只打我的助攻,在最佳的時(shí)機(jī),...
    thisDong閱讀 10,614評(píng)論 0 4
  • 第二節(jié):路由介紹 1.生成新項(xiàng)目 ng new 項(xiàng)目名 --routing : 注意 --routing的作...
    咖啡浮點(diǎn)閱讀 1,176評(píng)論 0 2
  • 官網(wǎng)鏈接: angular官網(wǎng) 路由與導(dǎo)航最好是跟著官網(wǎng)寫(xiě)一遍代碼,然后來(lái)看這個(gè)總結(jié),會(huì)比較清晰 如何實(shí)現(xiàn)一個(gè)簡(jiǎn)單...
    H_DaYan閱讀 3,304評(píng)論 0 6

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