【问题标题】:Angular ngStyle角度 ngStyle
【发布时间】:2021-09-13 20:44:51
【问题描述】:

我对 ngStyle 有疑问,我根据悬停变量放置样式。它有效,但我一直看到警告

Type {color: string, background: string, 'border-color': string} | string is not assignable to type {[p: string]: string} 

我的功能

getSuccessBtnStyleHover() {
        if (this.data && this.data.btnSBorderHColor && this.data.btnSBackgroundHColor && this.data.btnSTextHColor) {
            return {
                'color': this.data.btnSTextHColor,
                'background': this.data.btnSBackgroundHColor,
                'border-color': this.data.btnSBorderHColor,
            };
        }
        return '';
    }

我试过了

[ngStyle]="(hovered && themeService.getSuccessBtnStyleHover()) || (!hovered && themeService.getSuccessBtnStyle())"
                       (mouseover)="hovered = true"
                       (mouseout)="hovered = false"

[ngStyle]="hovered ? themeService.getSuccessBtnStyleHover() : themeService.getSuccessBtnStyle()"
                       (mouseover)="hovered = true"
                       (mouseout)="hovered = false"

问题出在哪里?

感谢您的帮助

enter image description here

【问题讨论】:

    标签: angular ng-style


    【解决方案1】:

    您将返回一个空字符串作为默认条件。所以返回类型要么是你的 CSS 对象,要么是一个空字符串。尝试返回{} 而不是''

    【讨论】:

    • 是的,同时我想通了,我实际上并没有返回一个对象......现在我有类似的东西:const obj: any = { 'color': ... }; return obj; 并且它有效:)
    猜你喜欢
    • 2019-11-20
    • 1970-01-01
    • 2020-10-14
    • 1970-01-01
    • 1970-01-01
    • 2020-09-28
    • 2021-02-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多