GWT CSSResrouces - 有什么优势或最好的方法
嘿,我正在开发一个 GWT 应用程序,现在面临 CSS 部分。我在官方网站上阅读了很多有关此主题的内容,但仍然有一些问题,希望有人能给我提示。
- 当我使用 CSSResource 时,CSS 样式将被编译到代码中 - 对吗?因此,如果不重新编译应用程序,就不可能更改它。但我想做的是让样式可以从“外部”编辑。
- 既然您不能在不再次编译应用程序的情况下更改颜色或图像,那么这个 CSSResource 是做什么用的呢?
- 使用 CSS 的最佳方法是什么,因为有几种方法(.css 文件、CSSResource、ui.xml 中的样式)可以做到这一点?
我现在的想法是,我只使用普通的 CSS 文件来处理所有“可更改”的内容并将此文件添加到头部,因为我看不到这个 CSSResource 东西的优点。希望有人能帮助我。谢谢。
Hey, I'm developing a GWT app and now facing the CSS part. I read a lot about this topic at the official site but still have a few questions and hope someone can give me a hint.
- When I'm using CSSResource the css styles will be compiled into the code - right? So it's not possible to change it without recompile the app. But what I wanna do is to have the styles be editable from "outside".
- So what is this CSSResource for, since you can't just change a color or an image without compiling the app again?
- Whats the best way to use CSS since there are a few ways (.css file, CSSResource, styles in ui.xml) to do this?
My thoughts are now, that I'm using only the normal CSS file to handle all my "changeable" stuff and add this file to the head, since I can't see the advantage of this CSSResource thing. Hopefully someone can help me with that. Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这完全取决于您使用 CSS 的方式 - 如果您需要应用一些小的更改,请首先在 Firebug/类似工具中“实时”测试它们。在设计阶段(当您还没有最终确定如何设计应用程序样式时),您可能希望使用普通 CSS 文件,但一旦总体样式明确,您应该切换到 ClientBundle/ CSSResource。
为什么?让我通过写一些关于 的想法来回答CssResource 文档中列出的目标 a> 页面:
主要
这并不意味着如果您只是在浏览器中显示样式表就一定有意义 - 意思是,我们只想使用有效的CSS语法,而不是一些对其他解析器来说是乱码的语法糖 - 可能并不那么重要从您的角度来看(尽管因此您不需要更改现有样式的语法)
Secondary
CssResource
),另外可能还有一个用于应用程序范围样式的全局 CssResource。这会产生更干净、更容易维护的 CSS 样式(至少根据我的经验)。这也意味着,如果您的代码不使用该特定的 Widget(可能是因为您使用了 ocde 分割并且尚未加载),您将不会下载这些样式。Tertiary
.warning
样式,并且编译器将理解这两种样式位于不同的命名空间(不同的 Widget)中,因此应该进行不同的处理(即,混淆为不同的样式)。名称)。关于样式注入
类
StyleInjector
允许在运行时注入样式。此外,CssResource 允许(至少根据文档,我还没有尝试过)运行时替换。例如,当您注入包含以下内容的CssResource
时:userBackground
将被评估并注入(而不是 常量,在运行时解析)。It all depends on the way you work with CSSes - if you need to apply some small changes, first test them "live", in Firebug/similar tool. During the designing phase (when you don't have a finalized view of how you want to style your application yet), you might want to work with normal CSS files, but as soon as the general style clarifies, you should switch to ClientBundle/CssResource.
Why? Let me answer, by writing up some thought about the goals listed on CssResource's documentation page:
Primary
This does not imply that the stylesheet would necessarily make sense if you just displayed it in a browser - meaning, we want to only use valid CSS syntax, not some syntactic sugar that's gibberish to other parsers - might not be that important from your point of view (although, as a result, you don't need to change the syntax of your existing styles)
Secondary
CssResource
internally) plus probably one global CssResource for application-wide styles. This results in a much cleaner, easier to maintain CSS styles (at least, in my experience). This also means, that if your code doesn't use that particular Widget (maybe because you've used ocde splitting and it hasn't been loaded yet), you won't download those styles.Tertiary
.warning
style in two Widgets and the compiler will understand that these two styles are in different namespaces (different Widgets) and thus should be treated differently (that is, obfuscated to different names).About style injection
The class
StyleInjector
allows injecting styles at runtime. Additionally, theCssResource
allows (at least according to the docs, I haven't tried it yet) for runtime substitution. For example, when you inject aCssResource
containing the following:The
userBackground
will get evaluated and injected (as opposed to constants, which are resolved at runtime).