本文转自:https://blog.csdn.net/m0_37981481/article/details/79281879

由于想要一个好看的alert,于是去npm上搜了一下,手动捂脸,npm上的package是真的多。。。

挑选了一个简洁大方的ng-alerts,拿来和大家分享一下使用过程。

环境:vs code

ng-alerts有一个官方的demo与教程:https://jaspero.co/resources/projects/ng-alerts

 
 

1.官方demo 简介

demo在页面的底端,如下图所示,可以调整各种参数。
 
1. message是alert的内容
2. type是alert的类型,共success,warning,error,info四种。
 
3. overlay是指alert出来的时候使页面其他部分变暗得覆盖层
4. close on overlay click是指alert显示出来的时候,点一下页面其他变暗的部分可以关闭alert
5. close button是指关掉alert的按钮
6. alert duration是指alert显示的时间长短
 
 

2.使用步骤

根据官方教程一步步走:

安装@jaspero/ng2-alerts包

npm install --save @jaspero/ng2-alerts
 

在@NgModule中import进来JasperoAlertsModule模块

先import:

import { JasperoAlertsModule } from '@jaspero/ng2-alerts';
 
  1.  
    @NgModule({
  2.  
    imports: [
  3.  
    JasperoAlertsModule
  4.  
    ],
  5.  
    declarations: [AppComponent],
  6.  
    bootstrap: [AppComponent]
  7.  
    })
  8.  
    export class AppModule {}

在html中写上:

<jaspero-alerts [defaultSettings]="options"></jaspero-alerts>
 

顺便再html中创建一个test的按钮:

<button class="btn btn-priamry" (click)="alerttest()">test</button>
 

在你的component类的构造函数中注入AlertsService:

import {AlertsService} from '@jaspero/ng2-alerts';
 
constructor(private _alert: AlertsService) {}
 

然后就可以写一个函数来试一下alert了:

  1.  
    alerttest(){
  2.  
    <span class="space" style="text-indent:2em;line-height:inherit;"> </span>this._alert.create('info', 'This is a message');
  3.  
    }

如果想要设置一下alert的各种参数,官方提供了一个接口:

  1.  
    export interface AlertSettings {
  2.  
    overlay?: boolean;
  3.  
    overlayClickToClose?: boolean;
  4.  
    showCloseButton?: boolean;
  5.  
    duration?: number;
  6.  
    }

在这里我写了一个类继承了这个接口:

 先import:
import {AlertSettings} from '@jaspero/ng2-alerts';
 
 
 
  1.  
    export class alertsetting implements AlertSettings {
  2.  
    overlay?: boolean = true;
  3.  
    overlayClickToClose?: boolean = true;
  4.  
    showCloseButton?: boolean = true;
  5.  
    duration?: number = 5000;
  6.  
    constructor(overlay?: boolean,
  7.  
    overlayClickToClose?: boolean,
  8.  
    showCloseButton?: boolean,
  9.  
    duration?: number) {
  10.  
    this.overlay = overlay;
  11.  
    this.overlayClickToClose = overlayClickToClose;
  12.  
    this.showCloseButton = showCloseButton;
  13.  
    this.duration = duration;
  14.  
    }
  15.  
    }

给刚才那个调用再增加一个配置参数,把duration设为1000:

  1.  
    alerttest(){
  2.  
    <span class="space" style="text-indent:2em;line-height:inherit;"> </span>this._alert.create('info', 'This is a message',new alertsetting(true,true,true,1000));
  3.  
    }

版权声明:本文为博主原创文章,未经博主允许不得转载。

---------------------

本文来自 Sophie1797 的CSDN 博客 ,全文地址请点击:https://blog.csdn.net/m0_37981481/article/details/79281879?utm_source=copy

[转]angular2中ng alerts的使用教程的更多相关文章

  1. Angular杂谈系列1-如何在Angular2中使用jQuery及其插件

    jQuery,让我们对dom的操作更加便捷.由于其易用性和可扩展性,jQuer也迅速风靡全球,各种插件也是目不暇接. 我相信很多人并不能直接远离jQuery去做前端,因为它太好用了,我们以前做的东西大 ...

  2. php中的curl使用入门教程和常见用法实例

    摘要: [目录] php中的curl使用入门教程和常见用法实例 一.curl的优势 二.curl的简单使用步骤 三.错误处理 四.获取curl请求的具体信息 五.使用curl发送post请求 六.文件 ...

  3. Swift语言中如何使用JSON数据教程

    这是一篇翻译文章,原文出处:http://www.raywenderlich.com/82706/working-with-json-in-swift-tutorial Swift语言中如何使用JSO ...

  4. CSS从大图中抠取小图完整教程(background-position应用)

    CSS从大图中抠取小图完整教程(background-position应用)  转自: http://www.cnblogs.com/iyangyuan/archive/2013/06/01/3111 ...

  5. 如何在Angular2中使用Forms

    在Angular2中形成两个基本对象是Control和ControlGroup. 控制用户输入 Control 一个控制表示一个输入字段(ngFormControl) 一个控制可以绑定到一个input ...

  6. sql server中创建链接服务器图解教程

    转自sql server中创建链接服务器图解教程 1.展开服务器对象-->链接服务器-->右击"新建链接服务器" 注意:必须以数据库管理员身份登录(通常也就是sa帐号) ...

  7. MySQL中的联合索引学习教程

    MySQL中的联合索引学习教程 这篇文章主要介绍了MySQL中的联合索引学习教程,其中谈到了联合索引对排序的优化等知识点,需要的朋友可以参考下   联合索引又叫复合索引.对于复合索引:Mysql从左到 ...

  8. 在.Net Core中使用MongoDB的入门教程(二)

    在上一篇文章中,讲到了MongoDB在导入驱动.MongoDB的连接,数据的插入等. 在.Net Core中使用MongoDB的入门教程(一) 本篇文章将接着上篇文章进行介绍MongoDB在.Net ...

  9. 在.Net Core中使用MongoDB的入门教程(一)

    首先,我们在MongoDB的官方文档中看到,MongoDb的2.4以上的For .Net的驱动是支持.Net Core 2.0的. 所以,在我们安装好了MangoDB后,就可以开始MangoDB的.N ...

随机推荐

  1. JSP请求重定向与请求转发的区别

    请求重定向 客户端行为,response.sendRedirect(),从本质上讲等同于两次请求,前一次请求对象不会保存,地址栏URL会改变: 请求转发 服务器行为,request.getReques ...

  2. PHP字符串截取函数

    substr函数 描述:实现截取字符串 语法:string substr(string $string,int $start [, int $length ]) 说明:如果省略length,则返回从s ...

  3. pdf预览(pdf.js)

    开门见山,pdf.js是Mozilla(缩写MF或MoFo)全称Mozilla基金会,下面的插件.现在社区非常活跃. Mozilla是为支持和领导开源的Mozilla项目而设立的一个非营利组织 下载地 ...

  4. Codeforces Round #486 (Div. 3) D. Points and Powers of Two

    Codeforces Round #486 (Div. 3) D. Points and Powers of Two 题目连接: http://codeforces.com/group/T0ITBvo ...

  5. Android逆向之smali

    Android逆向之smali 头信息 smail文件前三行 .class <访问权限> [关键修饰字] <类名>; .super <父类名>; .source & ...

  6. 简单好用的时间选择插件My97datepicker

    我们经常会需要验证字符串的格式,比如密码长度范围.电子邮件格式.固定电话号码和手机号码格式等,这个时候我们经常会需要用到正则表达式.但是正则表达式用起来性能会低一点,所以在需要验证的时候能不使用正则表 ...

  7. php中 curl, fsockopen ,file_get_contents 三个函数

    赵永斌:有些时候用file_get_contents()调用外部文件,容易超时报错.换成curl后就可以.具体原因不清楚curl 效率比file_get_contents()和fsockopen()高 ...

  8. 【原创】【Android】揭秘 ART 细节 ---- Garbage collection

      背景 Dalvik :http://zh.wikipedia.org/wiki/Dalvik%E8%99%9A%E6%8B%9F%E6%9C%BA ART :http://source.andro ...

  9. hotmail 收不到邮件的问题

    之前用 hotmail 注册过一个 aws 账号,起了一个 ec2 的免费一年的 VPS,然后没怎么用,不久就把这事忘了. 直到有一天手机收到信用卡扣款消息,我就马上去登账号,却怎么也想不起密码来了, ...

  10. Java代码审计入门篇

    作者:i春秋核心白帽yanzmi 原文来自:https://bbs.ichunqiu.com/thread-42149-1-1.html 本期斗哥带来Java代码审计的一些环境和工具准备. Java这 ...