相思资源网 Design By www.200059.com

本文实例讲述了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内容

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

ts

在ngOninit函数中获取显示的参数即可

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

创建用来跳转至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中定义一个函数

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

四、调用

test(){
  this.popupSVC.toast({
    img: '弹出图片地址!',
    title: '弹出消息内容!',
    duration: 2000, //toast多久后消失,默认为500ms
  });
}

更多关于AngularJS相关内容感兴趣的读者可查看本站专题:《AngularJS指令操作技巧总结》、《AngularJS入门与进阶教程》及《AngularJS MVC架构总结》

希望本文所述对大家AngularJS程序设计有所帮助。

标签:
Angular,2,路由,自定义,弹出组件,toast

相思资源网 Design By www.200059.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
相思资源网 Design By www.200059.com

评论“Angular 2使用路由自定义弹出组件toast操作示例”

暂无Angular 2使用路由自定义弹出组件toast操作示例的评论...

RTX 5090要首发 性能要翻倍!三星展示GDDR7显存

三星在GTC上展示了专为下一代游戏GPU设计的GDDR7内存。

首次推出的GDDR7内存模块密度为16GB,每个模块容量为2GB。其速度预设为32 Gbps(PAM3),但也可以降至28 Gbps,以提高产量和初始阶段的整体性能和成本效益。

据三星表示,GDDR7内存的能效将提高20%,同时工作电压仅为1.1V,低于标准的1.2V。通过采用更新的封装材料和优化的电路设计,使得在高速运行时的发热量降低,GDDR7的热阻比GDDR6降低了70%。