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

angular4實現(xiàn)tab欄切換的方法示例

 更新時間:2017年10月21日 09:13:00   作者:smiles  
本篇文章主要介紹了angular4實現(xiàn)tab欄切換的方法示例,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧

管理系統(tǒng) tab 切換頁,是一種常見的需求,大概如下:

點擊左邊菜單,右邊顯示相應的選項卡,然后不同的選項卡面可以同時編輯,切換時信息不掉失!

用php或.net,java的開發(fā)技術,大概是切換顯示,然后加一個ifram來做到,或者通過ajax加載信息顯示相應的層.

但是如果用angular 要如何實現(xiàn)呢?第一個想法,是否可以用同樣的ifarm來實現(xiàn)呢?

第二個想到的是路由插座大概是這樣的

復制代碼 代碼如下:

<router-outlet name="main-content" (activate)="activate($event)" (deactivate)='onDeactivate($event)' ></router-outlet>

但都沒能實現(xiàn),于是在想一個簡單的tab頁面就這么難嗎?

或者真的沒有什么簡單的方法了嗎?

很長一段時間,沒有去管這個了

因為我知道自己對angular的理解和學習還不夠,于是就放下了很長一段時間,直到在知乎看到一篇文章

Angular路由復用策略

于是有了一種思路,花了半天的時間終于實現(xiàn)了anguar 4  tab 切換頁大概思路實現(xiàn)如下:

一、實現(xiàn) RouteReuseStrategy 接口自定義一個路由利用策略

SimpleReuseStrategy.ts代碼如下:

import { RouteReuseStrategy, DefaultUrlSerializer, ActivatedRouteSnapshot, DetachedRouteHandle } from '@angular/router';

export class SimpleReuseStrategy implements RouteReuseStrategy {

  public static handlers: { [key: string]: DetachedRouteHandle } = {}

  /** 表示對所有路由允許復用 如果你有路由不想利用可以在這加一些業(yè)務邏輯判斷 */
  public shouldDetach(route: ActivatedRouteSnapshot): boolean {
    return true;
  }

  /** 當路由離開時會觸發(fā)。按path作為key存儲路由快照&組件當前實例對象 */
  public store(route: ActivatedRouteSnapshot, handle: DetachedRouteHandle): void {
    SimpleReuseStrategy.handlers[route.routeConfig.path] = handle
  }

  /** 若 path 在緩存中有的都認為允許還原路由 */
  public shouldAttach(route: ActivatedRouteSnapshot): boolean {
    return !!route.routeConfig && !!SimpleReuseStrategy.handlers[route.routeConfig.path]
  }

  /** 從緩存中獲取快照,若無則返回nul */
  public retrieve(route: ActivatedRouteSnapshot): DetachedRouteHandle {
    if (!route.routeConfig) {
      return null
    }
    
    return SimpleReuseStrategy.handlers[route.routeConfig.path]
  }

  /** 進入路由觸發(fā),判斷是否同一路由 */
  public shouldReuseRoute(future: ActivatedRouteSnapshot, curr: ActivatedRouteSnapshot): boolean {
    return future.routeConfig === curr.routeConfig
  }
}

二、策略注冊到模塊當中:

import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { FormsModule } from '@angular/forms';
import { CommonModule as SystemCommonModule } from '@angular/common';
import { AppComponent } from './app.component';
import { AppRoutingModule,ComponentList } from './app.routing'
import { SimpleReuseStrategy } from './SimpleReuseStrategy';
import { RouteReuseStrategy } from '@angular/router';

@NgModule({
 declarations: [
  AppComponent,
  ComponentList
 ],
 imports: [
  BrowserModule,
  AppRoutingModule,
  FormsModule,
  SystemCommonModule
 ],
 providers: [
  { provide: RouteReuseStrategy, useClass: SimpleReuseStrategy }
 ],
 bootstrap: [AppComponent]
})
export class AppModule { }

上面兩步基本上實現(xiàn)了復用策略但要實現(xiàn)第一張效果圖,還是要做一些其它工作

三、定義路由添加一些data數(shù)據(jù)路由代碼如下:

import { NgModule } from '@angular/core';
import { Routes, RouterModule } from '@angular/router';
import { AboutComponent } from './home/about.component'
import { HomeComponent } from './home/home.component'
import { NewsComponent } from './home/news.component'
import { ContactComponent } from './home/contact.component'



export const routes: Routes = [
 { path: '', redirectTo: 'home', pathMatch: 'full', },
 { path: 'home', component: HomeComponent,data: { title: '首頁', module: 'home', power: "SHOW" } },
 { path: 'news',component: NewsComponent ,data: { title: '新聞管理', module: 'news', power: "SHOW" }},
 { path: 'contact',component: ContactComponent ,data: { title: '聯(lián)系我們', module: 'contact', power: "SHOW" }},
 { path: 'about', component: AboutComponent,data: { title: '關于我們', module: 'about', power: "SHOW" } },
];

@NgModule({
 imports: [RouterModule.forRoot(routes)],
 exports: [RouterModule]
})

export class AppRoutingModule { }

export const ComponentList=[
  HomeComponent,
  NewsComponent,
  AboutComponent,
  ContactComponent
]

四、在<router-outlet></router-outlet> component 實現(xiàn)路由事件  events,app.component代碼如下:

import { Component } from '@angular/core';
import { SimpleReuseStrategy } from './SimpleReuseStrategy';
import { ActivatedRoute, Router, NavigationEnd } from '@angular/router';
import { Title } from '@angular/platform-browser';
import 'rxjs/add/operator/filter';
import 'rxjs/add/operator/map';
import 'rxjs/add/operator/mergeMap';

@Component({
 selector: 'app-root',
 styleUrls:['app.css'],
 templateUrl: 'app.html',
 providers: [SimpleReuseStrategy]
})

export class AppComponent {
 
 //路由列表
 menuList: Array<{ title: string, module: string, power: string,isSelect:boolean }>=[];

 constructor(private router: Router,
  private activatedRoute: ActivatedRoute,
  private titleService: Title) {
 
  //路由事件
  this.router.events.filter(event => event instanceof NavigationEnd)
   .map(() => this.activatedRoute)
   .map(route => {
    while (route.firstChild) route = route.firstChild;
    return route;
   })
   .filter(route => route.outlet === 'primary')
   .mergeMap(route => route.data)
   .subscribe((event) => {
    //路由data的標題
    let title = event['title'];
    this.menuList.forEach(p => p.isSelect=false);
    var menu = { title: title, module: event["module"], power: event["power"], isSelect:true};
    this.titleService.setTitle(title);
    let exitMenu=this.menuList.find(info=>info.title==title);
    if(exitMenu){//如果存在不添加,當前表示選中
     this.menuList.forEach(p => p.isSelect=p.title==title);
     return ;
    } 
    this.menuList.push(menu);
   });
 }

 //關閉選項標簽
 closeUrl(module:string,isSelect:boolean){
  //當前關閉的是第幾個路由
  let index=this.menuList.findIndex(p=>p.module==module);
  //如果只有一個不可以關閉
  if(this.menuList.length==1) return ;

  this.menuList=this.menuList.filter(p=>p.module!=module);
  //刪除復用
  delete SimpleReuseStrategy.handlers[module];
  if(!isSelect) return;
  //顯示上一個選中
  let menu=this.menuList[index-1];
  if(!menu) {//如果上一個沒有下一個選中
    menu=this.menuList[index+1];
  }
  // console.log(menu);
  // console.log(this.menuList);
  this.menuList.forEach(p => p.isSelect=p.module==menu.module );
  //顯示當前路由信息
  this.router.navigate(['/'+menu.module]);
 }
}
import { Component } from '@angular/core';
import { SimpleReuseStrategy } from './SimpleReuseStrategy';
import { ActivatedRoute, Router, NavigationEnd } from '@angular/router';
import { Title } from '@angular/platform-browser';
import 'rxjs/add/operator/filter';
import 'rxjs/add/operator/map';
import 'rxjs/add/operator/mergeMap';

@Component({
 selector: 'app-root',
 styleUrls:['app.css'],
 templateUrl: 'app.html',
 providers: [SimpleReuseStrategy]
})

export class AppComponent {
 
 //路由列表
 menuList: Array<{ title: string, module: string, power: string,isSelect:boolean }>=[];

 constructor(private router: Router,
  private activatedRoute: ActivatedRoute,
  private titleService: Title) {
 
  //路由事件
  this.router.events.filter(event => event instanceof NavigationEnd)
   .map(() => this.activatedRoute)
   .map(route => {
    while (route.firstChild) route = route.firstChild;
    return route;
   })
   .filter(route => route.outlet === 'primary')
   .mergeMap(route => route.data)
   .subscribe((event) => {
    //路由data的標題
    let title = event['title'];
    this.menuList.forEach(p => p.isSelect=false);
    var menu = { title: title, module: event["module"], power: event["power"], isSelect:true};
    this.titleService.setTitle(title);
    let exitMenu=this.menuList.find(info=>info.title==title);
    if(exitMenu){//如果存在不添加,當前表示選中
     this.menuList.forEach(p => p.isSelect=p.title==title);
     return ;
    } 
    this.menuList.push(menu);
   });
 }

 //關閉選項標簽
 closeUrl(module:string,isSelect:boolean){
  //當前關閉的是第幾個路由
  let index=this.menuList.findIndex(p=>p.module==module);
  //如果只有一個不可以關閉
  if(this.menuList.length==1) return ;

  this.menuList=this.menuList.filter(p=>p.module!=module);
  //刪除復用
  delete SimpleReuseStrategy.handlers[module];
  if(!isSelect) return;
  //顯示上一個選中
  let menu=this.menuList[index-1];
  if(!menu) {//如果上一個沒有下一個選中
    menu=this.menuList[index+1];
  }
  // console.log(menu);
  // console.log(this.menuList);
  this.menuList.forEach(p => p.isSelect=p.module==menu.module );
  //顯示當前路由信息
  this.router.navigate(['/'+menu.module]);
 }
}

app.html 的代碼如下:

<div class="row">
 <div class="col-md-4">
  <ul>
   <li><a routerLinkActive="active" routerLink="/home">首頁</a></li>
   <li><a routerLinkActive="active" routerLink="/about">關于我們</a></li>
   <li><a routerLinkActive="active" routerLink="/news">新聞中心</a></li>
   <li><a routerLinkActive="active" routerLink="/contact">聯(lián)系我們</a></li>
  </ul>
 </div>
 <div class="col-md-8">
  <div class="crumbs clearfix">
   <ul>
     <ng-container *ngFor="let menu of menuList">
       <ng-container *ngIf="menu.isSelect">
         <li class="isSelect">
           <a routerLink="/{{ menu.module }}">{{ menu.title }}</a> 
           <span (click)="closeUrl(menu.module,menu.isSelect)">X</span> 
         </li>
       </ng-container>
       <ng-container *ngIf="!menu.isSelect">
         <li>
           <a routerLink="/{{ menu.module }}">{{ menu.title }}</a> 
           <span (click)="closeUrl(menu.module,menu.isSelect)">X</span> 
         </li>
       </ng-container>
     </ng-container>
   </ul>
  </div>
  <router-outlet></router-outlet>
 </div>
</div>

 整體效果如下:

最終點擊菜單顯示相應的標簽選中,可以切換編輯內(nèi)容,關閉標簽時,重新點擊菜單可以重新加載內(nèi)容。

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

相關文章

  • AngularJS中的Directive自定義一個表格

    AngularJS中的Directive自定義一個表格

    本篇文章給大家介紹在angularjs中自定義一個有關表格的directive,涉及到angularjs directive相關知識,對本文感興趣的朋友一起學習吧
    2016-01-01
  • Angular動態(tài)綁定樣式及改變UI框架樣式的方法小結

    Angular動態(tài)綁定樣式及改變UI框架樣式的方法小結

    AngularJS 是一個 JavaScript 框架。它是一個以 JavaScript 編寫的庫。這篇文章主要介紹了Angular動態(tài)綁定樣式及改變UI框架樣式的方法小結,需要的朋友可以參考下
    2018-09-09
  • 淺析Angular2子模塊以及異步加載

    淺析Angular2子模塊以及異步加載

    本篇文章主要介紹了淺析Angular2子模塊以及異步加載,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-04-04
  • 理解Angular數(shù)據(jù)雙向綁定

    理解Angular數(shù)據(jù)雙向綁定

    AngularJS誕生于2009年,由Misko Hevery 等人創(chuàng)建,后為Google所收購。這篇文章主要帶著大家理解Angular數(shù)據(jù)雙向綁定,感興趣的小伙伴們可以參考一下
    2016-01-01
  • AngularJS使用ng-repeat指令實現(xiàn)下拉框

    AngularJS使用ng-repeat指令實現(xiàn)下拉框

    這篇文章主要介紹了AngularJS使用ng-repeat指令實現(xiàn)下拉框的相關資料,非常不錯,感興趣的朋友一起看下吧,需要的朋友可以參考下
    2016-08-08
  • 使用Angular CLI生成路由的方法

    使用Angular CLI生成路由的方法

    這篇文章主要介紹了使用Angular CLI生成路由的方法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2018-03-03
  • 理解AngularJs指令

    理解AngularJs指令

    這篇文章主要介紹了AngularJs指令
    2015-12-12
  • AngularJS過濾器filter用法分析

    AngularJS過濾器filter用法分析

    這篇文章主要介紹了AngularJS過濾器filter用法,結合實例形式分析了過濾器filter的功能、分類、使用方法與相關注意事項,需要的朋友可以參考下
    2016-12-12
  • Angular.js自動化測試之protractor詳解

    Angular.js自動化測試之protractor詳解

    Protractor是一個建立在WebDriverJS基礎上的端到端(E2E)的AngularJS JavaScript Web應用程序測試框架,下面這篇文章主要給大家介紹了angular.js自動化測試之protractor的相關資料,需要的朋友可以參考下。
    2017-07-07
  • 使用AngularJS創(chuàng)建單頁應用的編程指引

    使用AngularJS創(chuàng)建單頁應用的編程指引

    這篇文章主要介紹了使用AngularJS創(chuàng)建單頁應用的編程指引,AngularJS是一款高人氣的JavaScript庫,需要的朋友可以參考下
    2015-06-06

最新評論

满城县| 吴堡县| 广东省| 沛县| 嘉黎县| 五原县| 芜湖市| 鹰潭市| 若尔盖县| 大丰市| 太康县| 依兰县| 滕州市| 黎川县| 化德县| 南漳县| 黄浦区| 涟水县| 库尔勒市| 额尔古纳市| 甘泉县| 西宁市| 新津县| 泰宁县| 万荣县| 弥渡县| 嵊州市| 馆陶县| 株洲市| 南京市| 巨野县| 哈巴河县| 新绛县| 施甸县| 平遥县| 望奎县| 阿合奇县| 台安县| 焦作市| 屏南县| 阜南县|