首页 > 解决方案 > 如何从 Angular 组件中读取 :root 中的 CSS 变量?

问题描述

我试着这样做,像这样:

this.elementRef.nativeElement.style['--config-use-images'];

document.documentElement.style.getPropertyValue('--config-use-images');

两者都没有工作。变量肯定存在。我可以在下的开发工具中看到它:root。当我在控制台中记录第一个时,我得到一个未定义的,第二个是一个空字符串。

标签: javascriptcssangular

解决方案


尝试这个:

getComputedStyle(document.documentElement).getPropertyValue('--config-use-images')

或者

getComputedStyle(this.elementRef.nativeElement).getPropertyValue('--config-use-images')

推荐阅读