溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

Angular中路由及其用法的示例

發(fā)布時(shí)間:2021-03-03 11:27:44 來源:億速云 閱讀:261 作者:小新 欄目:web開發(fā)

這篇文章主要介紹了Angular中路由及其用法的示例,具有一定借鑒價(jià)值,感興趣的朋友可以參考下,希望大家閱讀完這篇文章之后大有收獲,下面讓小編帶著大家一起了解一下。

一、 Angular 創(chuàng)建一個(gè)默認(rèn)帶路由的項(xiàng)目

1、命令創(chuàng)建項(xiàng)目

ng new ng-demo --skip-install

Angular中路由及其用法的示例

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>

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

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

三、Angular routerLinkActive 設(shè)置 routerLink 默認(rèn)選中路由

<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>

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

4.1.問號(hào)傳參

跳轉(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);
 })
}

4.2 路徑傳參

路徑傳參的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í)!

向AI問一下細(xì)節(jié)

免責(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)容。

AI