Angular 6使用Angular CLI生成组件时无法生成.scss文件求助
Hey there! I totally get the frustration when the old config trick doesn't work in newer Angular versions. Angular 6 changed up the structure of angular.json, so the defaults.styleExt setting you tried is no longer valid here. Let's get this sorted step by step:
Step 1: Update your angular.json configuration
In Angular 6+, the style extension setting lives under the schematics section of your project's config, not the old defaults block. Here's how to set it up:
- Open the
angular.jsonfile in your project's root directory. - Locate your project's entry under the
projectsobject (it should match your project name). - If there's no
schematicskey inside your project's block, add it. - Add the component-specific schematic config to enforce
.scssstyles:
"projects": { "your-project-name": { // ... other existing config like architect, etc. "schematics": { "@schematics/angular:component": { "style": "scss" } } } }
Step 2: Verify the change
Save the angular.json file, then run a component generation command like:
ng generate component test-component
You should now see a test-component.component.scss file created alongside your component, instead of .css.
Bonus: Override for single components (if needed)
If you ever need to generate a component with a different style extension just once, you can use the --style flag directly in the command:
ng g c temp-component --style=css
That should do the trick! Let me know if you run into any hiccups.
内容的提问来源于stack exchange,提问作者pooja patil

