简体   繁体   English

组件在 .scss 文件中找不到变量

[英]Component can't find variable in .scss file

Angular.json snippet: Angular.json片段:

"root": "",
  "sourceRoot": "src",
  "prefix": "ra",
  "architect": {
    "build": {
      "builder": "@angular-devkit/build-angular:browser",
      "options": {
        "outputPath": "dist/",
        "index": "src/index.html",
        "main": "src/main.ts",
        "polyfills": "src/polyfills.ts",
        "tsConfig": "tsconfig.app.json",
        "aot": false,
        "assets": [
          "src/assets",
          "src/favicon.ico",
        ],
        "styles": [
          "src/styles.scss"
        ],
        "stylePreprocessorOptions": {
          "includePaths": ["assets/scss"]
        },
      }

New to Angular and I think I have an easy problem but just don't understand the inner workings of how scss is compiled. Angular新手,我认为我有一个简单的问题,但只是不了解scss编译方式的内部工作原理。 I'm importing some scss in my root styles.scss component.我在我的根样式.scss组件中导入了一些scss Here's what's in the styles.scss :这是style.scss中的内容

@import './assets/scss/global';

Here's what's in the _global.scss :这是_global.scss 中的内容

@import './variables';

And here's the contents of that file:这是该文件的内容:

$spacer-4: 0.25em; //4px
$spacer-6: 0.375em; //6px
$spacer-8: 0.5em; //8px
$spacer-10: 0.625em; //10px
$spacer-12: 0.75em; //12px
$spacer-16: 1em; //16px
$spacer-20: 1.25em; //20px
$spacer-24: 1.5em; //24px
$spacer-30: 1.875em; //30px
$spacer-32: 2em; //32px
$spacer-36: 2.25em; //36px

The problem is within a component's scss file that references one of the variables.问题出在引用变量之一的组件的 scss 文件中。 The error is thrown from the component:错误是从组件抛出的:

ERROR in ./src/navigation/app-navigation/app-navigation.component.scss    

Module build failed (from ./node_modules/sass-loader/lib/loader.js):

margin-top: $spacer-10;
           ^
  Undefined variable.

If I'm not getting a compile time error that the file isn't found then I shouldn't be seeing this error, right?如果我没有收到找不到文件的编译时错误,那么我不应该看到这个错误,对吧?

Here's the file structure of the app:这是应用程序的文件结构:

-- src
    -- app
        -- navigation
            --app-navigation
                -- app-navigation.component.scss
    -- assets
        -- scss
            -- _global.scss
            -- _variables.scss
    -- styles.scss

Your problem is the order of inception.你的问题是开始的顺序。 Your default styles.scss is the last in line in the order so your component doesn't have a reference to your variables/globals when it's inferred.您的默认 style.scss 是订单中的最后一行,因此您的组件在推断时没有对您的变量/全局变量的引用。

To fix it you just add an import reference to the component itself so it can receive those vars when it's needed.要修复它,您只需添加对组件本身的导入引用,以便它可以在需要时接收这些变量。 Which makes a good case for applying some presentation layer architecture rules to your uses.这是将一些表示层架构规则应用于您的用途的好例子。 Such as with variables, I would suggest keeping it a file with ONLY vars since they will not transpile and then you can reference it where those vars are needed and not have any duplicated CSS as you would if you had any actual CSS in the globals file in the compiled assets.例如对于变量,我建议将其保留为仅包含变量的文件,因为它们不会转译,然后您可以在需要这些变量的地方引用它,并且没有任何重复的 CSS,就像在全局文件中有任何实际 CSS 一样在编译的资产中。

I suggest if you haven't already add the stylePreprocessorOptions to your build configuration in angular.json with a fixed ref path to your directory that holds files like global so you don't have to make absolute paths each time you use it (don't forget to also add the declaration to your "serve" configuration in angular.json) like;我建议您是否还没有将stylePreprocessorOptions添加到stylePreprocessorOptions中的构建配置中,并在您的目录中添加一个固定的 ref 路径,该路径包含像 global 这样的文件,这样您就不必每次使用它时都创建绝对路径(不要不要忘记将声明添加到 angular.json 中的“服务”配置中),例如;

"stylePreprocessorOptions": {
   "includePaths": [
      "assets/sass/"
    ]
},

Then in say your component scss file you only add @import 'variables' and it will provide the var values as necessary.然后在说你的组件 scss 文件中,你只添加@import 'variables' ,它会根据需要提供 var 值。 Alternatively you could do the whole path but that can make maintenance a pain but will work just make sure your path is correct @import '../../variables';或者,您可以完成整个路径,但这可能会使维护变得痛苦,但只会确保您的路径正确@import '../../variables'; and with that in your component scss it will provide the vars also but I strongly recommend setting up the import paths in angular.json.并且在您的组件 scss 中,它也将提供变量,但我强烈建议在 angular.json 中设置导入路径

Hope this helps, cheers!希望这有帮助,干杯!

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

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