您好,登錄后才能下訂單哦!
這篇文章主要介紹了Angular中路由及其用法的示例,具有一定借鑒價(jià)值,感興趣的朋友可以參考下,希望大家閱讀完這篇文章之后大有收獲,下面讓小編帶著大家一起了解一下。
1、命令創(chuàng)建項(xiàng)目
ng new ng-demo --skip-install
2、創(chuàng)建需要的組件
ng g component components/home ng g component components/news ng g component components/newscontent
3、找到 app-routing.module.ts 配置路由
引入組件
import { HomeComponent } from './components/home/home.component'; import { NewsComponent } from './components/news/news.component'; import { ProductComponent } from './components/product/product.component';
配置路由
const routes: Routes = [ {path: 'home', component: HomeComponent}, {path: 'news', component: NewsComponent}, {path:'product', component:ProductComponent }, {path: '*', redirectTo: '/home', pathMatch: 'full' } ];
4、找到 app.component.html 根組件模板,配置 router-outlet 顯示動(dòng)態(tài)加載的路由
<h2> <a routerLink="/home">首頁</a> <a routerLink="/news">新聞</a> </h2> <router-outlet></router-outlet>
<a routerLink="/home">首頁</a> <a routerLink="/news">新聞</a>
//匹配不到路由的時(shí)候加載的組件 或者跳轉(zhuǎn)的路由 { path: '**', /*任意的路由*/ // component:HomeComponent redirectTo:'home' }
<h2> <a routerLink="/home" routerLinkActive="active"> 首頁 </a> <a routerLink="/news" routerLinkActive="active"> 新聞 </a> </h2>
<h2> <a [routerLink]="[ '/home' ]" routerLinkActive="active">首頁</a> <a [routerLink]="[ '/news' ]" routerLinkActive="active">新聞</a> </h2>
跳轉(zhuǎn)方式,頁面跳轉(zhuǎn)或js跳轉(zhuǎn)
問號(hào)傳參的url地址顯示為 …/list-item?id=1
queryParams屬性是固定的
<a [routerLink]="[’/list-item’]" [queryParams]="{id:item.id}">
{{ item.name }}
//js跳轉(zhuǎn)
//router為ActivatedRoute的實(shí)例
import { Router } from '@angular/router'; . constructor(private router: Router) {} . this.router.navigate(['/newscontent'],{ queryParams:{ name:'laney', id:id }, skipLocationChange: true //可以不寫,默認(rèn)為false,設(shè)為true時(shí)路由跳轉(zhuǎn)瀏覽器中的url會(huì)保持不變,傳入的參數(shù)依然有效 });
獲取參數(shù)方式
import { ActivatedRoute } from '@angular/router'; constructor(public route:ActivatedRoute) { } ngOnInit() { this.route.queryParams.subscribe((data)=>{ console.log(data); }) }
路徑傳參的url地址顯示為 …/list-item/1
<a [routerLink]="[’/list-item’, item.id]"> {{ item.name }} //js跳轉(zhuǎn) //router為ActivatedRoute的實(shí)例 this.router.navigate([’/list-item’, item.id]);
路徑配置:
{path: ‘list-item/:id’, component: ListItemComponent}
獲取參數(shù)方式
this.route.params.subscribe( param => { this.id= param['id']; } )
1、創(chuàng)建組件引入組件
import { WelcomeComponent } from ‘./components/home/welcome/welcome.component’; import { SettingComponent } from ‘./components/home/setting/setting.component’;
2、配置路由
{ path:'home', component:HomeComponent, children:[{ path:'welcome', component:WelcomeComponent },{ path:'setting', component:SettingComponent }, {path: '**', redirectTo: 'welcome'} ] },
3、父組件中定義router-outlet
感謝你能夠認(rèn)真閱讀完這篇文章,希望小編分享的“Angular中路由及其用法的示例”這篇文章對(duì)大家有幫助,同時(shí)也希望大家多多支持億速云,關(guān)注億速云行業(yè)資訊頻道,更多相關(guān)知識(shí)等著你來學(xué)習(xí)!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。