Angular 2使用路由自定义弹出组件toast操作示例
程序员文章站
2022-03-21 19:33:14
本文实例讲述了angular 2使用路由自定义弹出组件toast操作。分享给大家供大家参考,具体如下:
原理:
使用angular2的命名路由插座,一个用来显示app...
本文实例讲述了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="{{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函数中获取显示的参数即可
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 }); }
上一篇: 人参茯苓各自具有什么营养价值