亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

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

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

 實現(xiàn)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

實現(xiàn)效果:

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

  // 表單 雙向數(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默認加載組件就是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">聯(lián)系我們</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同理

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

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

相關文章

  • AngularJs bootstrap搭載前臺框架——基礎頁面

    AngularJs bootstrap搭載前臺框架——基礎頁面

    本文主要介紹AngularJs bootstrap搭載前臺框架基礎頁面的建設,這里整理餓了相關資料及實現(xiàn)實例代碼,有興趣的小伙伴可以參考下
    2016-09-09
  • Angular應用Bootstrap過程步驟邏輯詳解

    Angular應用Bootstrap過程步驟邏輯詳解

    這篇文章主要為大家介紹了Angular應用Bootstrap過程步驟邏輯詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2023-07-07
  • AngularJS模塊學習之Anchor Scroll

    AngularJS模塊學習之Anchor Scroll

    這篇文章主要介紹了AngularJS模塊學習之Anchor Scroll 的相關資料,需要的朋友可以參考下
    2016-01-01
  • AngularJS改變元素顯示狀態(tài)

    AngularJS改變元素顯示狀態(tài)

    本文主要介紹使用AngularJS提供的ng-show和ng-hide指令實現(xiàn)自動監(jiān)聽某布爾型變量來改變元素顯示狀態(tài)。下面跟著小編一起來看下吧
    2017-04-04
  • angular實現(xiàn)input輸入監(jiān)聽的示例

    angular實現(xiàn)input輸入監(jiān)聽的示例

    今天小編就為大家分享一篇angular實現(xiàn)input輸入監(jiān)聽的示例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2018-08-08
  • 對Angular.js Controller如何進行單元測試

    對Angular.js Controller如何進行單元測試

    這篇文章主要給大家介紹了如何對Angular Controller進行單頁測試。如果你不太了解angular也沒關系,本文也會提及關于Angular的一些知識。文中通過示例代碼介紹的很詳細,詳細對大家的理解和學習很有幫助,下面來一起看看吧。
    2016-10-10
  • angular8和ngrx8結合使用的步驟介紹

    angular8和ngrx8結合使用的步驟介紹

    這篇文章主要給大家介紹了關于angular8和ngrx8結合使用的詳細步驟,文中通過示例代碼介紹的非常詳細,對大家學習或者使用angular8具有一定的參考學習價值,需要的朋友們下面來一起學習學習吧
    2019-12-12
  • 淺談angular.js中實現(xiàn)雙向綁定的方法$watch $digest $apply

    淺談angular.js中實現(xiàn)雙向綁定的方法$watch $digest $apply

    Angular用戶都想知道數(shù)據(jù)綁定是怎么實現(xiàn)的。你可能會看到各種各樣的詞匯:$watch,$apply,$digest它們是如何工作的呢?這里我想回答這些問題,其實它們在官方的文檔里都已經回答了,但是我還是想把它們結合在一起來講
    2015-10-10
  • Angularjs中ng-repeat的簡單實例

    Angularjs中ng-repeat的簡單實例

    這篇文章主要介紹了Angularjs中ng-repeat的簡單實例的相關資料,這里提供兩個實例方法幫助大家徹底掌握這部分內容,需要的朋友可以參考下
    2017-08-08
  • angular使用post、get向后臺傳參的問題實例

    angular使用post、get向后臺傳參的問題實例

    本篇文章主要介紹了angular使用post、get向后臺傳參的問題實例。小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-05-05

最新評論