我是靠谱客的博主 强健百褶裙,这篇文章主要介绍Angular 2使用路由自定义弹出组件toast操作示例,现在分享给大家,希望可以做个参考。

本文实例讲述了Angular 2使用路由自定义弹出组件toast操作。分享给大家供大家参考,具体如下:

原理:

使用Angular2的命名路由插座,一个用来显示app正常的使用,一个用来显示弹出框,

复制代码
1
2
<router-outlet name='apps'></router-outlet> <router-outlet name='popup'></router-outlet>

浏览器的导航栏中则这样显示

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

路由配置

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
const rootRoute: Routes = [ { path: 'lists', component: Lists, outlet: 'apps', children: [ ... ] }, { path: 'toast', component: Toast, outlet: 'popup', }, ... ];

toast内容

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
<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函数中获取显示的参数即可

复制代码
1
this.toastParams = this.popupSVC.getToastParams();

创建用来跳转至popup路由的服务,例如popup.service

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
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中将服务导进来,注册

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
import { PopupService } from './svc/popup.service'; @NgModule({ imports: [ ... ], declarations: [ ... ], providers: [ PopupService, ... ], bootstrap: [AppComponent] })

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

复制代码
1
2
3
4
import { PangooService } from './svc/pangoo.service'; constructor( private popupSVC: PopupService, ) { }

三、在html中定义一个函数

复制代码
1
<div (click)="test()"></div>

四、调用

复制代码
1
2
3
4
5
6
7
test(){ this.popupSVC.toast({ img: '弹出图片地址!', title: '弹出消息内容!', duration: 2000, //toast多久后消失,默认为500ms }); }

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

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

最后

以上就是强健百褶裙最近收集整理的关于Angular 2使用路由自定义弹出组件toast操作示例的全部内容,更多相关Angular内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(123)

评论列表共有 0 条评论

立即
投稿
返回
顶部