最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

詳解angular2實現ng2-router 路由和嵌套路由

 更新時間:2017年03月24日 14:05:25   作者:zxc19890923  
本篇文章主要介紹了詳解angular2實現ng2-router 路由和嵌套路由,小編覺得挺不錯的,現在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧

 實現ng2-router路由,嵌套路由

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

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

實現效果:

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

開始配置

index.html界面配置兩點

<head>標簽中引入 <meta href="/" rel="external nofollow" />

引入路由代碼顯示標簽 引入主組件標簽 <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";

  // 表單 雙向數據綁定
  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默認加載組件就是AppComponent,所以他就是主頁導航頁,然后添加的路由都在他的模板中。

  // 可以所有代碼寫在NgModule中, 也可以這樣自定義常量,然后使用。

  // 定義常量 嵌套自路由
  const appChildRoutes: Routes = [
   {path: "one", component: ListOneComponent},
   {path: "two", component: ListTwoComponent},
   // 如果地址欄中輸入沒有定義的路由就跳轉到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 {
  
  }

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

顯示路由內容

app.component.ts

  import {Component} from "@angular/core";
  @Component({
   selector: "my-app",
   // templateUrl: "../views/one.html"
   template: `
        <div>
        <!--使用了bootstrap樣式的導航,routerLinkActive,表示路由激活的時候,談價active類樣式-->
         <ul class="nav navbar-nav">
          <li routerLinkActive="active"><a routerLink="home">首頁</a></li>
          <li routerLinkActive="active"><a routerLink="contact">聯系我們</a></li>
          <li routerLinkActive="active"><a routerLink="product">產品</a></li>
         </ul>
         <!--路由內容顯示區(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>
        <!-- 路由內容顯示標簽 -->
        <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同理

獲取路由參數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"]
補助: 路由中的界面跳轉
  import {Router} from "@angular/router";
  
  constructor(public router: Router) {
  // 相當于window.location.href,界面跳轉
    router.navigateByUrl('home');
  }

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。

相關文章

最新評論

桃园市| 双桥区| 林口县| 宜川县| 兴海县| 东城区| 桐城市| 永兴县| 台南市| 太康县| 扬中市| 东港市| 靖州| 登封市| 儋州市| 周宁县| 邹城市| 施秉县| 斗六市| 习水县| 灌云县| 固原市| 米林县| 贵南县| 巴马| 西安市| 沙洋县| 澄迈县| 睢宁县| 永宁县| 牙克石市| 九龙县| 游戏| 乐都县| 库伦旗| 湖口县| 陆川县| 古浪县| 阜城县| 荣成市| 襄垣县|