溫馨提示×

溫馨提示×

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

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

Angular 2使用路由自定義彈出組件toast操作示例

發(fā)布時間:2020-10-12 11:46:13 來源:腳本之家 閱讀:199 作者:zhy前端攻城獅 欄目:web開發(fā)

本文實例講述了Angular 2使用路由自定義彈出組件toast操作。分享給大家供大家參考,具體如下:

原理:

使用Angular2的命名路由插座,一個用來顯示app正常的使用,一個用來顯示彈出框,

<router-outlet name='apps'></router-outlet>
<router-outlet name='popup'></router-outlet>

瀏覽器的導航欄中則這樣顯示

http://127.0.0.1:4200/(popup:toast//apps:login)

路由配置

const rootRoute: Routes = [
{
  path: 'lists',
  component: Lists,
  outlet: 'apps',
  children: [ ... ]
},
{
  path: 'toast',
  component: Toast,
  outlet: 'popup',
},
...
];

toast內(nèi)容

<div class="box">
  <div class="toast-box">
    <p class="toast-title">提示</p>
    <div class="toast-content">
      <ng-container *ngIf="toastParams.img">
        <img src="{{toastParams.img}}" class="toast-content-img">
      </ng-container>
      <ng-container *ngIf="toastParams.title">
        <p class="toast-content-p">{{toastParams.title}}</p>
      </ng-container>
    </div>
  </div>
</div>

ts

在ngOninit函數(shù)中獲取顯示的參數(shù)即可

this.toastParams = this.popupSVC.getToastParams();

創(chuàng)建用來跳轉至popup路由的服務,例如popup.service

import { Injectable } from '@angular/core';
import { Router } from '@angular/router';
@Injectable()
export class PopupService {
  private toastParams;
  private loadingParams;
  constructor(
    private router: Router
  ) { }
  toast(_params) {
    this.toastParams = _params;
    let _duration;
    if (_params.duration) {
      _duration = _params.duration;
    } else {
      _duration = 500;
    }
    const _outlets = { 'popup': 'toast' };
    this.router.navigate([{ outlets: _outlets }]);
    setTimeout(() => {
      const _outlets2 = { 'popup': null };
      this.router.navigate([{ outlets: _outlets2 }]);
    }, _duration);
  }
  getToastParams() {
    return this.toastParams;
  }
}

使用:

一、在app.module.ts中將服務導進來,注冊

import { PopupService } from './svc/popup.service';
@NgModule({
  imports: [
    ...
  ],
  declarations: [
  ...
  ],
  providers: [
    PopupService,
  ...
  ],
  bootstrap: [AppComponent]
})

二、在使用的test.ts中導入

import { PangooService } from './svc/pangoo.service';
constructor(
  private popupSVC: PopupService,
) { }

三、在html中定義一個函數(shù)

<div (click)="test()"></div>

四、調(diào)用

test(){
  this.popupSVC.toast({
    img: '彈出圖片地址!',
    title: '彈出消息內(nèi)容!',
    duration: 2000, //toast多久后消失,默認為500ms
  });
}

更多關于AngularJS相關內(nèi)容感興趣的讀者可查看本站專題:《AngularJS指令操作技巧總結》、《AngularJS入門與進階教程》及《AngularJS MVC架構總結》

希望本文所述對大家AngularJS程序設計有所幫助。

向AI問一下細節(jié)

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

AI