angular6 使用信息提示框toast

需求描述:

   在操作结束后,需要在页面右下角弹出提示信息,且在一定的时间可以自动消失。

现有环境:Angular6+bootStrap

先看效果:

方案:选取了ngx-toastr这个插件

集成步骤:

1、安装ngx-toastr和它的依赖@angular/animations

npm install ngx-toastr --save
npm install @angular/animations --save

2、在angular.json中添加css样式

"styles": [
  "styles.scss",
  "node_modules/ngx-toastr/toastr.css"
]

3、在app.module中导入相关模块

import { BrowserAnimationsModule } from '@angular/platform-browser/animations';
import { ToastrModule } from 'ngx-toastr';import { AppComponent } from './app.component';
 
@NgModule({
  imports: [
    BrowserAnimationsModule,
    ToastrModule.forRoot()
  ],
  bootstrap: [AppComponent],
  declarations: [AppComponent]
})
class AppModule {}

4、业务代码中使用toast

import { ToastrService } from 'ngx-toastr';
 
@Component({...})
export class YourComponent {
  constructor(private toastr: ToastrService) {}
 
  showSuccess() {
    this.toastr.success('Hello world!', 'Toastr fun!');
  }
}

可以设置消失时间为3秒:

this.toastr.success('Hello world!', 'Toastr fun!', {timeOut: 3000})

提示信息默认在右上角位置,我们希望在右下角:

this.toastrService.success(message, '', {positionClass: 'toast-bottom-right'});

这里需要注意几个坑:

1.toastr的版本和Angular的版本是有对应要求的。所以根据自己的Angular版本安装对应的ngx-toastr,否则会出问题

2、还是版本的问题,直接npm install xxx的时候会下载最新的版本,而实际开发中,我们的Angular版本和其核心版本可能都是特定的,因此此处需要留意,我是在这搞了半天。。。

 3、还是版本的问题,没办法,这个坑实在太坑。。在解决版本的过程中,碰到如下问题

 原因:

        是因为rxjs版本问题造成的

解决方案:

使用npm:

npm uninstall rxjs --save

npm install rxjs@6.0.0 --save

 另外,记得若手动修改了package.json的依赖版本号后,记得npm install重新安装

参考文档:

ngx-toastrGit上的说明: https://www.npmjs.com/package/ngx-toastr

网上的安装说明:https://www.cnblogs.com/lucky-heng/p/11129462.html

版本错误处理办法:https://www.cnblogs.com/Julie-ye/p/12034711.html

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值