简体   繁体   English

在Angular 2/4中使用SCSS的体系结构

[英]Architecture for using SCSS with Angular 2/4

I am trying to setup an architecture for using SCSS with my angular 4 project rather than plain CSS . 我正在尝试为我的angular 4项目(而非普通CSS设置使用SCSS的体系结构。 Uptil now i have followed the Angular Style guide to arrange CSS simply adjacent to components ts files with each component having a separate directory, as in following the feature first approach. 到目前为止,我一直遵循Angular Style指南 ,将CSS与组件ts文件紧邻放置,每个组件具有单独的目录,如遵循功能优先方法一样。

Since now i am moving to using SCSS , i simple replaced my CSS files with SCSS files. 从现在开始,我开始使用SCSS ,我简单地用SCSS文件替换了CSS文件。 But i am not able to understand, where exactly should i put my _variables.scss , _mixins.scss , and common styles.scss files, such that variables are always the first to load, so that variables dependencies can be resolved in the SCSS files. 但是我不明白,我应该将_variables.scss_mixins.scss和常见的styles.scss文件放在哪里,这样变量始终是第一个被加载的变量,以便可以在SCSS文件中解析变量的依赖关系。 。

I am using webpack to compile my SCSS files. 我正在使用webpack编译我的SCSS文件。

webpack.config.js webpack.config.js

module: {
    rules: [
        {
            test: /\.ts$/,
            loaders: ['awesome-typescript-loader', 'angular2-template-loader']
        },
        {
            test: /\.html$/,
            loader: 'html-loader'
        },
        {
            test: /\.(png|jpe?g|gif|svg|woff|woff2|ttf|eot|ico)$/,
            loader: 'file-loader?name=assets/[name].[ext]'
        },
        {
           test: /\.scss$/,
           use: [
                { loader: "style-loader" },
                { loader: "css-loader" },
                { loader: "sass-loader" }
            ]
        }
    ]}

Since i am not very good at designing and architecturing an app, any help would be highly appreciated. 由于我不是很擅长设计和架构应用程序,因此我们将不胜感激。

If you want some common style code, and you're already using Webpack, then you can simply import the common styles into your main module. 如果您需要一些通用样式代码,并且已经在使用Webpack,则只需将通用样式导入到主模块中即可。 For example: 例如:

Directory Structure 目录结构

/src
  /app
    app.component.html
    app.component.ts
    app.component.scss
  /styles
    _mixins.scss
    _variables.scss
    common.scss
  app.module.ts
  main.ts

In your app.module.ts file you can import './styles/common.scss' , which will basically have all of your mixins and such imported into it. 在您的app.module.ts文件中,您可以import './styles/common.scss' ,该文件将基本上将您所有的mixin及其import './styles/common.scss'导入其中。 Your app.component.scss file will likely want to import "../styles/variables" and perhaps some mixins too. 您的app.component.scss文件可能要导入“ ../styles/variables”,也许还需要导入一些mixins。

The other option to having common stylesheets is to simply import the parts whenever you need them into component *.scss files rather than bringing them all in at once. 拥有通用样式表的另一种选择是,只要需要,就可以简单地将零件导入到* .scss组件文件中,而不是一次将它们全部放入。 This could prevent you from importing parts that you don't really need. 这可能会阻止您导入不需要的零件。

Someone developed a seed angular application with considering SASS and perfect architecture and everything else one might need. 有人开发了一个种子角度应用程序,考虑了SASS和完美的体系结构以及其他可能需要的所有功能。

Have fun exploring it. 玩得开心。 This Worked wonders for me. 这为我创造了奇迹。

Angular Library Seed 角库种子

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM