简体   繁体   English

如何使用 ng2-izitoast 将自定义 CSS 应用于 Toast?

[英]How can I apply custom CSS to a Toast with ng2-izitoast?

I want to apply a custom Style to the Toasts I show with izitoast in my Angular application.我想将自定义样式应用于我在 Angular 应用程序中使用 izitoast 显示的 Toast。 I have installed and included the library according to instructions here I have included the izitoast css and script in my angular.json in addition to adding the module in my app-module and also managed to display a toast with a button in a component:我已经根据此处的说明安装并包含了该库,除了在我的 app-module 中添加模块之外,我还在 angular.json 中包含了 izitoast css 和脚本,并且还设法在组件中显示带有按钮的 toast:

html-template (toaster.component.html) html-模板 (toaster.component.html)

<p>toaster works!</p>
<div><button (click)="produceToast()">Test Toast</button></div>

corresponding typescript file (toaster.component.ts)对应的打字稿文件(toaster.component.ts)

import { Component, OnInit } from '@angular/core';
import {Ng2IzitoastService} from "ng2-izitoast";

@Component({
  selector: 'app-toaster',
  templateUrl: './toaster.component.html',
  styleUrls: ['./toaster.component.scss']
})
export class ToasterComponent implements OnInit {

  constructor( public iziToast: Ng2IzitoastService) { }

  ngOnInit() {
  }

  public produceToast() {
    this.iziToast.show({ title: "Hello World"});
  }

}

I understand that if I want to apply custom styling I have to somehow specify the class in the object I pass into the the show() -function, but how do I do that?我知道如果我想应用自定义样式,我必须以某种方式指定传递给show()函数的对象中的类,但我该怎么做? Writing a CSS-class in my toaster.component.css and just referencing the name doesn't work:在我的 toaster.component.css 中编写一个 CSS 类并仅引用名称不起作用:

.myOwnToastClass {
  background-color: blueviolet;
  color: white; //font-color
}

Adding the class into my styles.css doesn't work either.将该类添加到我的 styles.css 中也不起作用。 Neither {class: "myOwnToastClass", title: "Hello World"} nor {class: ".myOwnToastClass", title: "Hello World"} do anything. {class: "myOwnToastClass", title: "Hello World"}{class: ".myOwnToastClass", title: "Hello World"}都不做任何事情。 Can someone tell me how to pass my own custom CSS to a toast?有人能告诉我如何将我自己的自定义 CSS 传递给 toast 吗? The documentation simply says "class: The class that will be applied to the toast. It may be used as a reference."文档只是说“类:将应用于 toast 的类。它可以用作参考。” but other than that there is no documentation on how to use it.但除此之外,没有关于如何使用它的文档。

Okay.好的。 So thanks to user fridoo I was able resolve it.所以感谢用户 fridoo 我能够解决它。

You have to add the class to your styles.css and be careful about the !important modifier:您必须将该类添加到您的 style.css 并注意 !important 修饰符:

.myOwnToastClass {
  background-color: blueviolet !important;
  border-radius: 0 !important;
  //color: white; // you can't change the font-color with this
                  // you have to use the object-properties in the ts-file
}

Then reference it in the typescript files like so:然后在打字稿文件中引用它,如下所示:

public produceToast() {
    this.iziToast.show({class: "myToastClass", title: "Hello World", timeout: 3000, titleColor: "#ffffff"});
        // titleColor: "white" would also work, I think it's a css-class somewhere in the background
  }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM