简体   繁体   English

Angular 组件的全局 scss 变量,无需每次都导入它们

[英]Global scss variables for Angular components without importing them everytime

I do already have SCSS variables defined in src/styles/settings/_variables.scss and I am importing them into src/styles.scss , but still these variables aren't available for every single component.我已经在src/styles/settings/_variables.scss中定义了 SCSS 变量,并且我将它们导入到src/styles.scss中,但这些变量仍然不适用于每个组件。

Is there any way to make a global file which holds all SCSS variables for the rest of my components?有没有办法制作一个全局文件来保存我的组件的 rest 的所有 SCSS 变量? Because writing @import in every single component .scss file it is very frustrating, especially if I have many nested components.因为在每个单独的组件.scss文件中编写@import非常令人沮丧,尤其是在我有许多嵌套组件的情况下。

I know, there is a lot of similar questions, but it seems like they're all outdated and do not relate to the recent versions of Angular.我知道,有很多类似的问题,但似乎它们都已过时,并且与 Angular 的最新版本无关。

I use Angular 7.3 with CLI.我将 Angular 7.3 与 CLI 一起使用。

You just need to add a little more config, so where you are declaring your global variables, you need to wrap it up in :root{} .您只需要添加更多配置,因此在声明全局变量的地方,您需要将其包装在:root{} So in src/styles/settings/_variables.scss .所以在src/styles/settings/_variables.scss

:root 
{
--blue: #00b; // or any global you wish to share with components 
}

Then when you use them in the SCSS you will need to access them like so.然后当您在 SCSS 中使用它们时,您将需要像这样访问它们。

.example-class {
  background-color: var(--blue)
}

To add to this regarding comments, this method can use mixins , @media and keyframes and is not limited to just colours / font.要添加关于评论的内容,此方法可以使用mixins 、@ @mediakeyframes ,而不仅限于颜色/字体。 That was an example.那是一个例子。

From my understanding you need a global file src/assets/style/global and then to import each scss file into there where you are defining them like so.根据我的理解,您需要一个全局文件src/assets/style/global ,然后将每个 scss 文件导入到您定义它们的位置。

@import 'filename';

If you dont want global variables to be used in within a component look when you have the globals working.如果您不希望在组件中使用全局变量,请在全局变量工作时查看。 Look into ViewEncapsulation , as this can be used to ignore them.查看ViewEncapsulation ,因为这可用于忽略它们。

Is there any ways to make global file with scss variables available for all components?有什么方法可以使具有scss 变量的全局文件可用于所有组件?

Without importing global file everytime in each component, you want those sass variables been available, it's not possible.无需在每个组件中每次都导入全局文件,您希望这些sass 变量可用,这是不可能的。

The way it works in SASS , if using partials to better organize code, you can apply @import directive for referencing.它在SASS 中的工作方式,如果使用部分来更好地组织代码,您可以应用@import指令进行引用。 So if there're some sass variables in shared/_variables.scss :因此,如果shared/_variables.scss有一些 sass 变量:

$lightslategray: #778899;
$darkgray: #A9A9A9;

and these variables need to be used in another stylesheet, stylesheet with them must be @import-ed into it firstly:并且这些变量需要在另一个样式表中使用,带有它们的样式表必须首先@import-ed 到其中:

// Shared
@import "shared/variables";

.content {
  background: $lightslategray;
}

In Angular it works in a similar way (related referencing external stylesheet).在 Angular 中,它以类似的方式工作(相关引用外部样式表)。 So if you need some sass variables, mixins or functions to be used by a particular component.scss , there is no other clean way, but to reference them in that component.scss using @import directive.因此,如果您需要一些sass variables, mixins or functions供特定的component.scss ,则没有其他干净的方法,只能使用@import指令在该component.scss引用它们。 To ease the task, you can create a file src/_variables.scss and use syntax like this in your component.scss :为了简化任务,您可以创建一个文件src/_variables.scss并在您的component.scss使用这样的语法:

@import “~variables.scss”;

Easily possibe to access sass style(s) from a global file with two steps.只需两步即可从全局文件轻松访问 sass 样式

  1. Add folder path of the style files to includePaths array in angular.json file.将样式文件的文件夹路径添加到angular.json文件中的includePaths数组中。
  2. Import style file by file-name in any component.在任何组件中按文件名导入样式文件。

let say your files and folder structures is as follows: src > my-styles-folder > var.scss假设您的文件和文件夹结构如下: src > my-styles-folder > var.scss

angular.json

"architect": {
  "build": {
    ...
    "options": {
      "stylePreprocessorOptions": {
        "includePaths": [
          "src/my-styles-folder" // add path only, do not include file name
        ]
      },
      "styles": [
        ...
      ]
    }
    ...
  }
}

some-component.scss

@import "var"; // var.scss

mat-toolbar {
  height: $toolbar-height;
}

step one : go to custom scss file (shared/css/_variable.scss) and write this part第一步:转到自定义scss文件(shared/css/_variable.scss)并编写这部分

 :root{ --color-text: red; --color-btn-success: green; }

after go to style.scss (this is main file) and import this file :转到 style.scss (这是主文件)并导入此文件后:

 @import './shared/css/Variables';

now you can use variables in all components with this Syntax:现在您可以使用以下语法在所有组件中使用变量:

 .sample{ color : var(--color-text); }

In angular 8 work for me.在 angular 8 为我工作。

In your _variable.scss file you have to add:在您的 _variable.scss 文件中,您必须添加:

:root{--my-var:#fabada}

After that go in your angular.json and add this in "styles":之后进入你的angular.json并将其添加到“样式”中:

{"input":"yourPath/_variables.scss"}

TL;DR TL; DR

No, there is no better way in Angular and there will be no more convenient way to solve your problem. 不,在Angular中没有更好的方法,也没有更方便的方法来解决您的问题。

More detailed answer 更详细的答案

If I understood you correctly, you're messing up with all these dependency injection, for instance @import a specific SCSS holding your variables. 如果我对您的理解正确,那么您就搞砸了所有这些依赖项注入,例如@import一个保存变量的特定SCSS。 So, your question is: 因此,您的问题是:

Is this a bug or a feature? 这是错误还是功能?

And, I have to say, it's a feature. 而且,我不得不说,这是一个功能。 This makes your components more flexible and the payoff is, that you have to care about these dependencies, but in the long run your code will be easier to maintain and will be more structured. 这使您的组件更加灵活,需要付出的代价是,您必须关心这些依赖关系,但是从长远来看,您的代码将更易于维护和更加结构化。 Dependency is a core feature of modern JavaScript programming. 依赖关系是现代JavaScript编程的核心功能。 So, if you like, you can stick to more old-fashioned style of programming, putting everything you need for your application into one single file. 因此,如果您愿意,可以坚持使用更老式的编程风格,将应用程序所需的所有内容都放在一个文件中。 This is more kind of rethorical question. 这是一个比较现实的问题。

If you look for instance to the „React world“, you will find even more necessity of structuring your injections. 如果您将目光投向“反应世界”,您会发现构造注射剂的必要性更高。 You do not just import a whole file and serve all variables, methods, classes etc. to your target component, instead you choose each single variable, each single method, which you'd like to use in your target component. 您不仅可以导入整个文件并将所有变量,方法,类等提供给目标组件,还可以选择要在目标组件中使用的每个单个变量,每个方法。 This increases the effort of planning and structuring your whole application, but keep in mind the long run. 这会增加规划和构建整个应用程序的工作量,但请记住长远来看。 Programming good maintainable software has more in common with a marathon than a sprint. 编写良好的可维护软件与马拉松的共同点在于冲刺。

I'd like to share with you this Wikipedia article about „Dependency Injection“ . 我想与您分享有关“依赖注入”的Wikipedia文章 Maybe you'll find your answers there. 也许您会在这里找到答案。

Motivation 动机

To prevent you from leaving you with bad feelings, here is a wonderful saying of HL Mencken : 为了防止您心情不好,这是HL Mencken的一句好话

„For every complex problem there is an answer that is clear, simple, and wrong.“ “对于每个复杂的问题,都有一个清晰,简单和错误的答案。”

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

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