【问题标题】:Angular: Global Variables on Styles scss to share among all componentsAngular:样式 scss 上的全局变量以在所有组件之间共享
【发布时间】:2022-01-25 14:43:17
【问题描述】:

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

我的变量文件:

$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;

我的项目结构:

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

感谢您的帮助!

【问题讨论】:

  • 如果我理解正确,您希望在所有文件中都可以使用 sass 变量,而无需直接导入变量文件。在我看来,它不起作用,因为 angular.json 配置中的样式参数是在构建时包含的,而不是您期望可用于预处理器的参数。我工作的大多数项目在每个 scss 文件导入变量和函数的开头都有一个导入。
  • 在这个例子中,pantaley.com/blog/… 虽然添加了stylePreprocessorOptions,但他仍在使用导入。它只有缩短网址的好处
  • 嗨@Simplicity's_Strength 我认为这正是我想要做的。这些帖子解释说,他将颜色文件导入到 styles.scss 中,这样他就可以在任何他想要的地方使用它们。但这就是我想要做的,但仍然失败:(
  • @Simplicity's_Strength 不过,我仍然想让它以这种方式工作。

标签: angular sass global


【解决方案1】:

为了使用 css 中的变量,您必须执行以下操作:

  1. 在您的“_variables.scss”中,在 :root* 中定义您的变量,类似于:
:root {
    // Your colors    
   --color-primary-color: #efefef;
   --color-light: #454758;
   --color-main-font:  #ACADB4;
   --color-primary-white: #FFFFFF;
... 
}
  1. 在“styles.scss”中,您已经很好地导入了变量文件:
/* VARIABLES */
@import "styles/_variables.scss";

  1. 然后,在您的 styles.scss(或您选择的另一个 scss 文件)中,将这些变量与 var() 一起使用,如下所示:
color: var(--color-primary-white);

我留给你HERE 一个链接,指向一篇关于 CSS 自定义属性 (vars) 和 SASS/SCSS(一种实用的架构策略)的文章

【讨论】:

  • 您好,感谢您的回复!是的,我在其他帖子中看到过这种方式,但我不太喜欢它,因为我习惯使用导入,并找到了几篇使它起作用的帖子。这和我提议的方式有什么不同吗?
  • --myvar 是 css 原生变量的示例,它们在运行时工作并且可以在运行时被覆盖。目前它是首选变量,除非您支持像 IE11 这样的旧浏览器。在此功能在所有浏览器中运行之前,scss $variables 的存在是为了部分填充它们,现在它们不是必需的
猜你喜欢
  • 2019-08-17
  • 1970-01-01
  • 2018-02-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-02-28
  • 2015-03-28
  • 1970-01-01
相关资源
最近更新 更多