Angular:样式 scss 上的全局变量在所有组件之间共享

标签 angular sass global

我知道这是一个非常常见的问题,但我似乎找不到设置项目的正确方法。在 Angular 12 中,我尝试设置一个变量文件以在所有组件之间共享,而不将其导入每个 scss 文件中,并且我尝试在 styles.scss 文件中设置它,但似乎并没有工作正常。我收到“ undefined variable ”错误。

我的变量文件:

$primary-color: #efefef;
$light-color: #454758;
$main-font:  #ACADB4;
$main-font-regular: #6E707D;
$main-font-light: #3c3c3c;
$rich-black: #001011;
$primary-white: #fafafa;

以下是我将其导入 styles.scss 的方法:

/* FONT AWESOME */
@import "~@fortawesome/fontawesome-free/scss/fontawesome.scss";
@import "~@fortawesome/fontawesome-free/scss/solid.scss";
@import "~@fortawesome/fontawesome-free/scss/regular.scss";
@import "~@fortawesome/fontawesome-free/scss/brands.scss";

/* MDB CSS */
@import "~mdb-angular-ui-kit/assets/scss/mdb.scss";

/* VARIABLES */
@import "styles/_variables.scss";

我的 angular.json 文件:

{
  "$schema": "./node_modules/@angular/cli/lib/config/schema.json",
  "version": 1, 
  "newProjectRoot": "projects",
  "projects": {
    "CVWebPage": {
      "projectType": "application",
      "schematics": {
        "@schematics/angular:component": {
          "style": "scss"
        }
      },
      "root": "",
      "sourceRoot": "src",
      "prefix": "app",
      "architect": {
        "build": {
          "builder": "@angular-devkit/build-angular:browser",
          "options": {
            "outputPath": "dist/CVWebPage",
            "index": "src/index.html",
            "main": "src/main.ts",
            "polyfills": "src/polyfills.ts",
            "tsConfig": "tsconfig.app.json",
            "assets": [
              "src/favicon.ico",
              "src/assets"
            ],
            "styles": [
              "node_modules/bootstrap/dist/css/bootstrap.min.css", 
              "src/styles.scss",
              "src/styles/_variables.scss"
            ],
            "stylePreprocessorOptions": {
              "includePaths": [
                "./src/styles" 
              ]
            },
            "scripts": [
              "node_modules/jquery/dist/jquery.min.js",
              "node_modules/popper.js/dist/umd/popper.js",
              "node_modules/bootstrap/dist/js/bootstrap.min.js"
            ],
            "vendorChunk": true,
            "extractLicenses": false,
            "buildOptimizer": false,
            "sourceMap": true,
            "optimization": false,
            "namedChunks": true
          },
          "configurations": {
            "production": {
              "fileReplacements": [
                {
                  "replace": "src/environments/environment.ts",
                  "with": "src/environments/environment.prod.ts"
                }
              ],
              "optimization": true,
              "outputHashing": "all",
              "sourceMap": false,
              "namedChunks": false,
              "extractLicenses": true,
              "vendorChunk": false,
              "buildOptimizer": true,
              "budgets": [
                {
                  "type": "initial",
                  "maximumWarning": "2mb",
                  "maximumError": "5mb"
                },
                {
                  "type": "anyComponentStyle",
                  "maximumWarning": "6kb",
                  "maximumError": "10kb"
                }
              ]
            }
          },
          "defaultConfiguration": ""
        },
        "serve": {
          "builder": "@angular-devkit/build-angular:dev-server",
          "options": {
            "browserTarget": "CVWebPage:build"
          },
          "configurations": {
            "production": {
              "browserTarget": "CVWebPage:build:production"
            }
          }
        },
        "extract-i18n": {
          "builder": "@angular-devkit/build-angular:extract-i18n",
          "options": {
            "browserTarget": "CVWebPage:build"
          }
        },
        "test": {
          "builder": "@angular-devkit/build-angular:karma",
          "options": {
            "main": "src/test.ts",
            "polyfills": "src/polyfills.ts",
            "tsConfig": "tsconfig.spec.json",
            "karmaConfig": "karma.conf.js",
            "assets": [
              "src/favicon.ico",
              "src/assets"
            ],
            "styles": [
              "node_modules/bootstrap/dist/css/bootstrap.min.css",
              "src/styles.scss",
              "src/styles/_variables.scss"
            ],
            "scripts": [
              "./node_modules/jquery/dist/jquery.slim.min.js",
              "./node_modules/popper.js/dist/umd/popper.min.js",
              "./node_modules/bootstrap/dist/js/bootstrap.min.js"
            ]
          }
        },
        "lint": {
          "builder": "@angular-devkit/build-angular:tslint",
          "options": {
            "tsConfig": [
              "tsconfig.app.json",
              "tsconfig.spec.json",
              "e2e/tsconfig.json"
            ],
            "exclude": [
              "**/node_modules/**"
            ]
          }
        },
        "e2e": {
          "builder": "@angular-devkit/build-angular:protractor",
          "options": {
            "protractorConfig": "e2e/protractor.conf.js",
            "devServerTarget": "CVWebPage:serve"
          },
          "configurations": {
            "production": {
              "devServerTarget": "CVWebPage:serve:production"
            }
          }
        }
      }
    }},
  "defaultProject": "CVWebPage"
}

我只是在组件 scss 文件中使用它,如下所示:

color: $primary-white;

我的项目结构:

enter image description here

我尝试做与这篇文章相同的操作,但无法使其工作:How to use global scss files in angular-cli project

感谢您的帮助!

最佳答案

为了在CSS中使用变量,你必须这样做:

  1. 在“_variables.scss”中,在 :root* 中定义变量,例如:
:root {
    // Your colors    
   --color-primary-color: #efefef;
   --color-light: #454758;
   --color-main-font:  #ACADB4;
   --color-primary-white: #FFFFFF;
... 
}
  • 在“styles.scss”中,您已经导入了变量文件:
  • /* VARIABLES */
    @import "styles/_variables.scss";
    
    
  • 然后,在您的 styles.scss(或您选择的另一个 scss 文件)中,将这些变量与 var() 一起使用,如下所示:
  • color: var(--color-primary-white);
    

    我离开你HERE关于 CSS 自定义属性(变量)与 SASS/SCSS(实用架构策略)的文章链接

    关于Angular:样式 scss 上的全局变量在所有组件之间共享,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70481841/

    相关文章:

    javascript - 数据绑定(bind) ng2 组件的模板仅设置 OnInit

    javascript - 在 Angular 中设置超时和 NgZone

    css - 居中 zurb 基础固定顶栏链接

    ios - 全局函数和 AppDelegate 类的 performSelector 错误

    当组件scss文件导入样式scss文件时,Angular组件的样式 View 封装不起作用

    c# - Angular2+ C# Web Api - 服务器端保存错误时间的日期时间

    css - Bootstrap SASS 生成供应商文件夹

    sass - @each 带索引的循环

    C# 使用全局键盘钩子(Hook)全局覆盖 CTRL+V 命令

    c - 静态全局变量和函数内部静态变量的区别