溫馨提示×

溫馨提示×

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

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

Angular中路由及其用法的示例

發布時間:2021-03-03 11:27:44 來源:億速云 閱讀:305 作者:小新 欄目:web開發

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

一、 Angular 創建一個默認帶路由的項目

1、命令創建項目

ng new ng-demo --skip-install

Angular中路由及其用法的示例

2、創建需要的組件

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 顯示動態加載的路由

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

二、Angular routerLink 跳轉頁面默認路由

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

三、Angular routerLinkActive 設置 routerLink 默認選中路由

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

四、動態路由

4.1.問號傳參

跳轉方式,頁面跳轉或js跳轉
問號傳參的url地址顯示為 …/list-item?id=1

queryParams屬性是固定的

<a [routerLink]="[’/list-item’]" [queryParams]="{id:item.id}">
{{ item.name }}

//js跳轉
//router為ActivatedRoute的實例

import { Router } from '@angular/router';
.
constructor(private router: Router) {}
.
this.router.navigate(['/newscontent'],{
  queryParams:{
    name:'laney',
    id:id
  },
  skipLocationChange: true 
  //可以不寫,默認為false,設為true時路由跳轉瀏覽器中的url會保持不變,傳入的參數依然有效
});

獲取參數方式

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跳轉 //router為ActivatedRoute的實例
this.router.navigate([’/list-item’, item.id]);

路徑配置:

{path: ‘list-item/:id’, component: ListItemComponent}

獲取參數方式

this.route.params.subscribe(
  param => {
      this.id= param['id'];
  }
)

五、父子路由

1、創建組件引入組件

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

感謝你能夠認真閱讀完這篇文章,希望小編分享的“Angular中路由及其用法的示例”這篇文章對大家有幫助,同時也希望大家多多支持億速云,關注億速云行業資訊頻道,更多相關知識等著你來學習!

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

亚洲午夜精品一区二区_中文无码日韩欧免_久久香蕉精品视频_欧美主播一区二区三区美女