我是Angular的新手,来自Ember社区。尝试使用新的基于Ember-CLI的Angular-CLI。

我需要知道在一个新的Angular项目中处理SASS的最佳方法。我尝试使用Ember-CLI -sass回购,看看它是否能正常运行,因为Angular-CLI的许多核心组件都是在Ember-CLI模块上运行的。

它没有工作,但比再次不确定,如果我只是配置错误的东西。

另外,在新的Angular项目中组织样式的最佳方法是什么?如果能将sass文件与组件放在同一个文件夹中就太好了。


当前回答

引用自官方github回购这里-

以安装为例 NPM安装node-sass 将项目中的.css文件重命名为.scss或.sass。它们将被自动编译。 Angular2App的options参数有sassCompiler、lessCompiler、stylusCompiler和compassCompiler选项,它们直接传递给各自的CSS预处理器。

在这里看到的

https://github.com/angular/angular-cli/wiki/stories-css-preprocessors

其他回答

针对Angular 6+的更新

新项目

当用Angular CLI生成一个新项目时,将css预处理器指定为

使用SCSS语法 Ng新的ssy-project——style=scss 使用SASS语法 Ng新的时髦项目——style=sass

更新现有项目

通过运行在现有项目上设置默认样式

使用SCSS语法 Ng config schematics.@schematics/angular:组件。styleext scss 使用SASS语法 Ng config schematics.@schematics/angular:组件。styleext sass 上面的命令将更新你的工作空间文件(angular.json) “图表”:{ “@schematics /角:组件":{ :“styleext scss” } }

其中styleext可以是SCSS或sass根据选择。

CSS预处理器集成 Angular CLI支持所有主要的CSS预处理程序:

要使用这些预处理器,只需将文件添加到组件的styleUrls中:

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.scss']
})
export class AppComponent {
  title = 'app works!';
}

当生成一个新项目时,你也可以定义样式文件的扩展名:

ng new sassy-project --style=sass

或者在现有项目上设置默认样式:

ng config schematics.@schematics/angular:component.styleext scss

注意:@schematics/angular是angular CLI的默认原理图

样式字符串添加到@Component。styles数组必须用CSS编写,因为CLI不能对内联样式应用预处理程序。

基于angular文档 https://github.com/angular/angular-cli/wiki/stories-css-preprocessors

就像Mertcan说的,使用scss的最好方法是创建带有该选项的项目:

ng new My_New_Project --style=scss 

Angular-cli还添加了一个选项,可以在项目创建后使用以下命令更改默认的css预处理器:

ng set defaults.styleExt scss

要了解更多信息,你可以在这里查看他们的文档:

https://github.com/angular/angular-cli

引用自官方github回购这里-

以安装为例 NPM安装node-sass 将项目中的.css文件重命名为.scss或.sass。它们将被自动编译。 Angular2App的options参数有sassCompiler、lessCompiler、stylusCompiler和compassCompiler选项,它们直接传递给各自的CSS预处理器。

在这里看到的

https://github.com/angular/angular-cli/wiki/stories-css-preprocessors

对于Angular 12,更新Angular。json:

"schematics": {
   "@schematics/angular:component": {
      "style": "scss"
    }
  }

and:

"build": {
  "options": {
    ...
    "inlineStyleLanguage": "scss"
  }
}

"test": {
  "options": {
    ...
    "inlineStyleLanguage": "scss"
  }
}