GWT - css 中常量的问题
我是 GWT 新手;我正在构建一个小型示例应用程序。我有几个 CSS 文件。我能够成功使用 ClientBundle 和 CssResource 将样式分配给 UiBinder 脚本中定义的元素。
现在我想更进一步,使用 @def css-rule 引入 CSS 常量。当我定义一个常量并在同一个 CSS 文件中使用它时,@def 效果很好。但是我不能在另一个 CSS 文件中使用它。当我尝试使用 @eval 规则来评估现有常量时,编译器会抛出一个异常:“无法对非静态方法进行静态引用”。
这是我想做的一个例子:
ConstantStyle.css
@def BACKGROUND red;
ConstantStyle.java
package abc;
import ...;
interface ConstantStyle extends cssResource {
String BACKGROUND();
}
MyStyle.css
@eval BACKGROUND abc.ConstantStyle.BACKGROUND();
.myClass {background-color: BACKGROUND;}
MyStyle.java
package abc;
import ...;
interface ConstantStyle extends cssResource {
String myClass;
}
MyResources.java
package abc;
import ...;
interface MyResources extends ClientBundle {
@Source("ConstantStyle.css")
ConstantStyle constantStyle();
@Source("MyStyle.css")
MyStyle myStyle();
}
提前致谢!
I'm new to GWT; I'm building a small sample app. I have several CSS files. I'm able to successfully use the ClientBundle and CssResource to assign styles to the elements defined in my UiBinder script.
Now I'd like to take it one step further and introduce CSS constants using @def css-rule. The @def works great when I define a constant and use it in the same CSS file. However I cannot use it in another CSS file. When I try to use the @eval rule to evaluate an existing constant the compiler throws an execption: "cannot make a static reference to the non-static method ".
Here is an example of what I'm trying to do:
ConstantStyle.css
@def BACKGROUND red;
ConstantStyle.java
package abc;
import ...;
interface ConstantStyle extends cssResource {
String BACKGROUND();
}
MyStyle.css
@eval BACKGROUND abc.ConstantStyle.BACKGROUND();
.myClass {background-color: BACKGROUND;}
MyStyle.java
package abc;
import ...;
interface ConstantStyle extends cssResource {
String myClass;
}
MyResources.java
package abc;
import ...;
interface MyResources extends ClientBundle {
@Source("ConstantStyle.css")
ConstantStyle constantStyle();
@Source("MyStyle.css")
MyStyle myStyle();
}
Thanks in advance!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
将
"ConstantStyle.css"
添加到ClientBundle
中的@Source
注释中,如下所示:它的作用与
@import "ConstantStyle 相同.css"
在 MyStyle.css 中(除了@import
被 GWT 忽略,我相信)。你不需要@eval。
Add
"ConstantStyle.css"
to the@Source
annotation inClientBundle
like this:It does the same as would
@import "ConstantStyle.css"
in MyStyle.css (except@import
is ignored by GWT I believe).You don't need @eval.
我在代码中执行此操作的方法是简单地引用
MyStyle.css
中的常量,即:我不完全确定为什么会这样,可能是因为两个 CSS 文件都以正确的顺序注入。否则,我尝试在
MyStyle.css
顶部添加以下内容,它也有效:The way I do this in my code is to simply refer to the constant within
MyStyle.css
, i.e.:I'm not entirely sure why that works, probably because both CSS files are injected in the right order. Otherwise, I tried adding the following at the top of
MyStyle.css
and it works too: