国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > JavaScript > 正文

詳解angular2實現(xiàn)ng2-router 路由和嵌套路由

2019-11-19 17:02:41
字體:
供稿:網(wǎng)友

 實現(xiàn)ng2-router路由,嵌套路由

首先配置angular2的時候router模塊已經(jīng)下載,只需要引入即可

import {RouterModule, Routes} from "@angular/router";

我們要創(chuàng)建一個嵌套路由,所以需要創(chuàng)建以下文件

  • index.html
  • app.module.ts
  • app.component.ts
  • home.component.ts
  • list.component.ts
  • list-one.component.ts
  • list-two.component.ts

實現(xiàn)效果:

  1. 路由,單機(jī)“首頁”加載home.component.ts
  2. 單機(jī)"列表“加載list.component.ts
  3. 列表中包含嵌套路由,tab頁
  4. 單機(jī)"標(biāo)簽一"加載list-one.component.ts
  5. 單機(jī)"標(biāo)簽二"加載list-one.component.ts

開始配置

index.html界面配置兩點

<head>標(biāo)簽中引入 <meta href="/" rel="external nofollow" />

引入路由代碼顯示標(biāo)簽 引入主組件標(biāo)簽 <my-app></my-app>

就這么簡單, index.html界面配置完畢

app.module.ts界面配置路由

  import {BrowserModule} from "@angular/platform-browser";  import {NgModule} from "@angular/core";  import {RouterModule, Routes} from "@angular/router";  // 表單 雙向數(shù)據(jù)綁定  import {FormsModule} from "@angular/forms";  import {AppComponent} from "./app.component";  // List中包含兩個tab子組件  import {ListComponent} from "./list.component";  import {ListOneComponent} from "./list-one.component";  import {ListTwoComponent} from "./list-two.component";  import {HomeComponent} from "./home.component";  // 定義路由, bootstrap默認(rèn)加載組件就是AppComponent,所以他就是主頁導(dǎo)航頁,然后添加的路由都在他的模板中。  // 可以所有代碼寫在NgModule中, 也可以這樣自定義常量,然后使用。  // 定義常量 嵌套自路由  const appChildRoutes: Routes = [   {path: "one", component: ListOneComponent},   {path: "two", component: ListTwoComponent},   // 如果地址欄中輸入沒有定義的路由就跳轉(zhuǎn)到one路由界面   {    path: '**', redirectTo: "one"   }  ];  // 定義常量 路由  const appRoutes:Routes = [   {path: '', component: HomeComponent},   {    path: 'list',    component: ListComponent,    children: appChildRoutes  ];  // 引用定義的路由  @NgModule({   imports: [    BrowserModule,    FormsModule,    RouterModule.forRoot(appRoutes)   ],   declarations: [    AppComponent,    ListComponent,    HomeComponent,    ListOneComponent,    ListTwoComponent   ],   bootstrap: [AppComponent]  })  export class AppModule {    }

這樣就完成了嵌套路由的配置

顯示路由內(nèi)容

app.component.ts

  import {Component} from "@angular/core";  @Component({   selector: "my-app",   // templateUrl: "../views/one.html"   template: `        <div>        <!--使用了bootstrap樣式的導(dǎo)航,routerLinkActive,表示路由激活的時候,談價active類樣式-->         <ul class="nav navbar-nav">          <li routerLinkActive="active"><a routerLink="home">首頁</a></li>          <li routerLinkActive="active"><a routerLink="contact">聯(lián)系我們</a></li>          <li routerLinkActive="active"><a routerLink="product">產(chǎn)品</a></li>         </ul>         <!--路由內(nèi)容顯示區(qū)域-->         <router-outlet></router-outlet>        </div>        `  })  export class AppComponent {    }

list.component.ts

  import {Component} from "@angular/core";  @Component({    selector: "my-list",    // templateUrl: "../views/list.html"    template: `       <div>        <!-- 子路由連接 -->        <a routerLink="one">one</a>        <a routerLink="two">two</a>        <!-- 路由內(nèi)容顯示標(biāo)簽 -->        <router-outlet></router-outlet>       </div>     `  })  export class ListComponent {    name = "list";  }

list-one.component.ts

  import {Component} from "@angular/core"  @Component({    selector: "my-list-one",    template:`      {{name}}    `  })  export class ListOneComponent {    name = "list-one";    }

list-two.component.ts同理

獲取路由參數(shù)id (about:id) 添加模塊 ActivatedRoute

  import {ActivatedRoute} from "@angular/router";    export class AboutList {    id: Object;    constructor(public route:ActivatedRoute) {      this.id = {};    }    ngOnInit() {      this.route.params.subscribe(params => {        this.id = params // {id: "xxx"}      });    }  }

直接獲取id值

  this.route.snapshot.params["id"]補(bǔ)助: 路由中的界面跳轉(zhuǎn)  import {Router} from "@angular/router";    constructor(public router: Router) {  // 相當(dāng)于window.location.href,界面跳轉(zhuǎn)    router.navigateByUrl('home');  }

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持武林網(wǎng)。

發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 遂昌县| 澄城县| 盘山县| 宜良县| 天水市| 高密市| 镇赉县| 即墨市| 收藏| 胶南市| 德安县| 孟村| 子长县| 突泉县| 时尚| 阳春市| 霞浦县| 临桂县| 正安县| 通山县| 富平县| 吐鲁番市| 巫溪县| 武夷山市| 灌阳县| 桃源县| 新蔡县| 仙桃市| 长岭县| 潞西市| 海口市| 林州市| 瑞金市| 时尚| 崇义县| 衡南县| 湖口县| 武安市| 库伦旗| 五河县| 晋江市|