[英]Set custom css variable using hostbinding
I am trying to make a component customizable by having the color as an input.我正在尝试通过将颜色作为输入来定制组件。 In the end, I want to be able to use the component like this
<component-name color="myColor"></component-name>
:最后,我希望能够像这样使用组件
<component-name color="myColor"></component-name>
:
To start with, I'm trying to set the css variable using hostbinding, like this:首先,我尝试使用主机绑定设置 css 变量,如下所示:
@HostBinding('style.--color') myColor: string = '--blue'
:host { --blue: #5dbbbb; --green: #286e22; background-color: var(--color); }
But the blue never renders.但蓝色从不渲染。 If I inspect the component in chrome, I can see that it has
"background-color: --color"
When I look at the variable it's defined as --color: --blue
如果我检查 chrome 中的组件,我可以看到它具有
"background-color: --color"
当我查看变量时,它被定义为--color: --blue
It works fine if I use the hex code directly (@HostBinding('style.--color') myColor: string = '#xxxxxx')
instead of the css variable for the color, but I want to avoid that if possible.如果我直接使用十六进制代码
(@HostBinding('style.--color') myColor: string = '#xxxxxx')
而不是颜色的 css 变量,它工作正常,但我想尽可能避免这种情况。 Any thoughts on what could be wrong?有什么想法可能是错的吗? I'm using Angular14.
我正在使用 Angular14。
I think you should change your HostBinding code我认为您应该更改 HostBinding 代码
this这个
@HostBinding('style.--color') myColor: string = '--blue'
to至
@HostBinding('style.--color') myColor: string = 'var(--blue)'
And it should work它应该工作
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.