【问题标题】:How to apply css to text inside an ionic alert?如何将css应用于离子警报中的文本?
【发布时间】:2019-05-31 16:21:58
【问题描述】:

我有一个警报,我有一些文本和一个离子图标,我想更改它的颜色并调整它的大小和位置(中心),我创建了一个 css 类尝试应用它,但它不起作用,甚至认为HTML 似乎可以工作,我使用 <b> 标签就好了。
我的代码:

async presentAlertError(data) {
    const alert = await this.alertController.create({
      header: 'Confirmation',
      message: '<ion-icon name="close" class="custom-icon-notfound" > </ion-icon>'  + "Registration " + data.statusText,
      buttons: [
        {
          text: 'Cancel',
          role: 'cancel',
          cssClass: 'secondary',
          handler: (aux) => {
            console.log('Confirm Cancel: aux');
          }
        }, {
          text: 'Scan Next',
          handler: () => {
            this.scanCode();
            console.log('Confirm ');
          }
        }
      ]
    });
    await alert.present()

我应该以不同的方式执行此操作吗?

【问题讨论】:

  • 次要类被添加到我的文本元素中很好,你能显示你试图添加的 CSS 吗?
  • css 是 .custom-icon-notfound {color: red}

标签: ionic-framework sass alert


【解决方案1】:

ion-alert 不会显示在主页或(无论您在哪个页面/组件上)shadow DOM 中,因此如果您在那里添加样式不会影响它,您必须将样式添加到其中的元素与警报范围相同。我将该类添加到 variables.scss 中,因为它具有最高范围:

variables.scss-

   .secondary {
            color: #A0A !important;
            font-size: 2em !important;
        }

   .custom-icon-notfound {color: red}

还需要添加 !important 以便它忽略之前的样式。

【讨论】:

  • 我确实在 home 的 css 中有这个(这是 home 组件,所以它在正确的关联文件上)我担心它不会工作,因为消息上的这个 html 是以编程方式制作的,我我在文本消息上添加了 html,在它之上还有一个 css 类。但这是正确的做法吗?
  • 抱歉,我以为问题是关于“中学”课程的。你这样做的方式很好,应该可以在 ios 和 android 上渲染。如果我这样做了,我可能会将图标作为类添加到消息中,而不是在消息中嵌入 html,但我认为这不是问题。
  • 现在我想到了你使用 ionic 的图标。老实说,除了您所做的方式之外,没有其他方式可以做到这一点。你搞定了吗?
  • 感谢这对我有用,将 css 移动到 variables.scss,这个文件位于 src/themes 位置,如果有人也需要这个 :)
  • @AntWo 也在 global.scss 中工作
猜你喜欢
  • 2018-05-23
  • 1970-01-01
  • 2022-07-08
  • 1970-01-01
  • 2021-11-11
  • 1970-01-01
  • 1970-01-01
  • 2018-05-16
  • 1970-01-01
相关资源
最近更新 更多