本文转自: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. 学习Tensorflow的LSTM的RNN例子

    学习Tensorflow的LSTM的RNN例子 基于TensorFlow一次简单的RNN实现 极客学院-递归神经网络 如何使用TensorFlow构建.训练和改进循环神经网络

  2. MySQL skip-character-set-client-handshake导致的一个字符集问题

    http://www.quweiji.com/mysql-skip-character-set-client-handshake%E5%AF%BC%E8%87%B4%E7%9A%84%E4%B8%80 ...

  3. mui 页面提示:Unable to preventDefault inside passive

    页面提示: 点击该事件:页面提示:[8mui.min.js:7 [Intervention] Unable to preventDefault inside passive event listene ...

  4. Note | LaTeX

    目录 一.TeX家族 1. TeX - LaTeX 2. pdfTeX - pdfLaTeX 3. XeTeX - XeLaTeX 4. CTeX - MiKTeX - TeX Live 二.入门 1 ...

  5. ASP.NET Core 请求/查询/响应参数格式转换(下划线命名)

    业务场景: 在 ASP.NET Core 项目中,所有的代码都是骆驼命名,比如userName, UserName,但对于 WebApi 项目来说,因为业务需要,一些请求.查询和响应参数的格式需要转换 ...

  6. php安装扩展的几种方法

    转自:http://doc3.workerman.net/appendices/install-extension.html 安装扩展 注意 与Apache+PHP或者Nginx+PHP的运行模式不同 ...

  7. Vue自定义指令,ref ,sync,slot

    一.自定义指令 vue中可以自己设置指令,通过directive来实现,有2种创建方式,一种是局部创建,一种是全局创建. 第一种:局部创建 如果想注册局部指令,组件中也接受一个 directives  ...

  8. InnoDB体系架构(一)后台线程

    InnoDB体系架构——后台线程 上一篇已经了解了MySQL数据库的体系结构 这一篇除了介绍InnoDB存储引擎的体系架构外,同时进一步了解InnoDB的后台线程. InnoDB存储引擎是多线程的模型 ...

  9. Markdown新手教程

    目录 什么是Markdown? 用Markdown写作有什么优缺点? 有哪些比较好的Markdown写作工具? markdown语法 标题 水平分区线 引用 中划线 斜体 粗体 斜粗体 链接 图片 无 ...

  10. keepalived-1

    keepalived所执行的外部脚本命令建议使用绝对路径 vrrp 广播 keepalived的主要功能 1,管理LVS负载均衡软件 2,对LVS集群节点健康检查功能.Healthcheck 3,