溫馨提示×

溫馨提示×

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

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

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

發(fā)布時(shí)間:2020-10-12 09:07:05 來源:腳本之家 閱讀:191 作者:zxc19890923 欄目:web開發(fā)

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

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

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

我們要?jiǎng)?chuàng)建一個(gè)嵌套路由,所以需要?jiǎ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

實(shí)現(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界面配置兩點(diǎn)

<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中包含兩個(gè)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,表示路由激活的時(shí)候,談價(jià)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í)有所幫助,也希望大家多多支持億速云。

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

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI