首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >ngx-toastr:如何在自定义吐司组件中获取当前的烤面包Id

ngx-toastr:如何在自定义吐司组件中获取当前的烤面包Id
EN

Stack Overflow用户
提问于 2020-07-09 14:51:51
回答 1查看 1.8K关注 0票数 2

按一下祝酒词内的一个按钮,我想要关闭那个特别的祝酒词。我想知道在下面的自定义土司组件中获取土司Id的方法是我的代码

代码语言:javascript
复制
action(btn: IToastButton) {
 enter code hereevent.stopPropagation();
 this.toastPackage.triggerAction(btn);
 this.toastrService.remove(toastID);
 return false;
}
EN

回答 1

Stack Overflow用户

发布于 2020-07-09 15:45:46

根据我的理解,您需要tostr的id,因为您需要关闭特定id的toastr。在这里,我有一个解决方案,您可以通过这个解决方案定制您想要的烤面包。

要做到这一点,您需要制作一个自定义toastr组件&在TOASTR中初始化。

component.html

代码语言:javascript
复制
<!-- custom toastr -->
<div class="row" [style.display]="state.value === 'inactive' ? 'none' : ''">
  <div class="col-9">
    <div *ngIf="title" [class]="options.titleClass" [attr.aria-label]="title">
      {{ title }}
    </div>
    <div *ngIf="message && options.enableHtml" role="alert" aria-live="polite"
      [class]="options.messageClass" [innerHTML]="message">
    </div>
    <div *ngIf="message && !options.enableHtml" role="alert" aria-live="polite"
      [class]="options.messageClass" [attr.aria-label]="message">
      {{ message }}
    </div>
  </div>
  <div class="col-3 text-right">
    <a *ngIf="options.closeButton" (click)="remove()" class="btn btn-sm font-medium-3">
      &#10005;
    </a>
  </div>
</div>
<div *ngIf="options.progressBar">
  <div class="toast-progress" [style.width]="width + '%'"></div>
</div>
<!--/ custom toastr -->

component.ts

代码语言:javascript
复制
import { Component } from '@angular/core';

import { Toast, ToastrService, ToastPackage } from 'ngx-toastr';
import { toastrSlideY } from './custom-toastr.animation';

@Component({
  selector: '[app-custom-toastr-component]',
  templateUrl: './custom-toastr.component.html',
  animations: [ toastrSlideY ],
  preserveWhitespaces: false
})
export class CustomToastrComponent extends Toast {

  constructor(
    protected toastrService: ToastrService,
    public toastPackage: ToastPackage
  ) {
    super(toastrService, toastPackage);
  }

}

toastr.component.ts

代码语言:javascript
复制
  // initializing custom toastr 
  toastr() {
    const customToastrRef = cloneDeep(this.options);
    customToastrRef.toastComponent = CustomToastrComponent;
    customToastrRef.closeButton = true;
    customToastrRef.tapToDismiss = false;
    this.toastr.success(
      'Have fun storming the castle!',
      'Slide Down / Slide Up!',
      customToastrRef
    );
  }

toastr.component.html

代码语言:javascript
复制
<button class="btn btn-outline-warning" (click)="toastr()">Toastr</button>

注意:确保在entery组件中添加自定义组件,如下所示:

module.ts

代码语言:javascript
复制
declarations: [
ToastrComponent,
CustomToastrComponent,
],
entryComponents: [CustomToastrComponent]
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/62817638

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档