我不是 Ember 大师,但我想加入一些有用的约定和工具,Ember 社区在我们拥抱基于组件的未来时正在采用关于样式表的一些有用的约定和工具。
注意要求:ember-cli
和scss
。
通过这篇文章:敏捷设计宣言。您无需将所有样式表插入到 pod 结构中即可获得好处,只要您...
“按路由和组件组织 SCSS”
对于组件,文章建议您希望保持全局选择:
> stylesheets/components/_flash_messages.scss
/*
Base Styling for the Flash Messages component - how it will appear globally.
*/
.flash-messages {
background-color: $default-flash-color;
}
对于资源,您可以利用 ID 选择器和 Ember 的约定来确保具有给定 ID 的模板只出现一次,并且您的 SCSS 代码可能如下所示:
> stylesheets/routes/_posts.scss
/*
Global Styling for the "Posts" resource.
It's an ID because it's guaranteed to only ever appear on the page once.
Thanks Ember!
*/
#posts {
@import "show";
@import "new";
@import "edit";
}
您可以使用它来覆盖全局样式并创建人造 CSS 范围。
导入的显示路线样式可以是:
> stylesheets/routes/posts/_show.scss
/*
Styling here is specifically for this on the "Show" route of the "Posts" resource.
Most likely, it's empty, but it's a good place to override the global appearance of components, and ensure those changes are contained to this route only.
*/
#posts-show {
.flash-messages {
background-color: $posts-show-flash-color;
}
}
鉴于这些建议,您可以使用类似的模块:ember-cli-sass-pods以允许您在路由或组件 pod 中生成样式表。然后,您需要将@import
声明添加到文件中生成的文件中app.scss
。