简体   繁体   English

SCSS / SASS中具有角度分量的全局变量

[英]Global variable in SCSS / SASS with angular components

We are developing a cloud application in angular 5 that will be deployed in a single instance but will be addressed to several customer. 我们正在开发角度为5的云应用程序,该应用程序将在单个实例中部署,但将面向多个客户。 The question concerns the management of themes, each customer wants to have his own theme (mainly colors). 问题涉及主题的管理,每个客户都希望拥有自己的主题(主要是颜色)。 The architecture (simplified) of the application looks like this: 应用程序的架构(简化)如下所示:

--src
    --app
    app.component.html
    app.component.scss
    app.component.ts
        -- component1
        comp1.component.html
        comp1.component.scss
        comp1.component.ts
    ...

--scss
    -- current 
        style.scss
        _variables.scsc
    -- customer1
        style.scss
        _variables.scss
    -- customer2
        style.scss
        _variables.scss
    ...

Currently we are deploying by client, so there is no problem, we copy / paste the style in the current before the build. 当前,我们正在按客户端进行部署,因此没有问题,我们在构建之前将样式复制/粘贴到当前样式中。

Each component.scss imports _variables.scss to exploit the variables. 每个component.scss都导入_variables.scss以利用变量。

We would like that when a user logs on to the app, we detect the customer and choose the right style, but that the css is generated at compile time. 我们希望当用户登录到应用程序时,我们检测到客户并选择了正确的样式,但是css是在编译时生成的。

Is there a way to define global variables that can be modified in runtime and that impacts sub-components? 有没有一种方法可以定义可以在运行时修改并且会影响子组件的全局变量?

The solutions I tested: 我测试的解决方案:

  • Set in angular-cli a scss per customer, build and execute script js to modify the html link to css "href = 'assets / {customer} .bundle.css'". 为每位客户设置一个angular-cli a scss,构建并执行脚本js以修改指向css“ href ='assets / {customer} .bundle.css'”的html链接。 It works for global styles, but variables in subcomponents are not updated. 它适用于全局样式,但是子组件中的变量不会更新。

  • Use pure css to declare scope variables: root {--color-primary: gray; 使用纯CSS声明范围变量:root {--color-primary:grey; } and exploit them in the sub-components .test {color: var (- color-primary)}. },并在子组件.test {color:var(-color-primary)}中加以利用。 Make a JS script that will update all the global variables according to the client. 制作一个JS脚本,该脚本将根据客户端更新所有全局变量。 It works, but no equivalent in SCSS? 它有效,但是在SCSS中没有等效功能吗? strange 奇怪

I do not know if I gave enough detail, thanks for the help. 我不知道是否提供了足够的细节,谢谢您的帮助。

There is no way to pass any variables from ts to scss. 无法将任何变量从ts传递到scss。

All you need is theming. 您所需要的只是主题。 So for each customer you need a global body class whith its own set of variables / classes. 因此,对于每个客户,您都需要一个全局主体类,并带有自己的一组变量/类。

Check out angular material theming docs for example https://material.angular.io/guide/theming#defining-a-custom-theme 查看角度材料主题文档,例如https://material.angular.io/guide/theming#defining-a-custom-theme

Mixins will solve your issue. Mixins将解决您的问题。

In the specific customer scss files, you would be holding the specific definition. 在特定的客户scss文件中,您将保留特定的定义。 In the component.scss, you would be using the mixin, which is specific to the customer and it will resolve your issue on both compile and run time. 在component.scss中,您将使用特定于客户的mixin,它将解决编译和运行时的问题。

As there is no proper way to do this; 由于没有适当的方法来执行此操作; And solution used by Angular theming was not satisfactory to us; Angular主题使用的解决方案对我们来说并不令人满意; we've decided to use custom webpack builder that will compile our style based on the entries we provide. 我们决定使用自定义Webpack构建器 ,该构建器将根据我们提供的条目来编译我们的样式。 Please note, that we are not using SCSS in angular components explicitly. 请注意,我们没有在角度组件中明确使用SCSS。

"use strict";

const MiniCssExtractPlugin = require("mini-css-extract-plugin");
const CopyWebpackPlugin = require("copy-webpack-plugin");
const AutoPrefixer = require("autoprefixer");

module.exports = {
  entry: {
    "modern_style.application": "./src/styles/modern_style.scss",
    "default.application": "./src/styles/default.scss"
  },
  plugins: [
    new MiniCssExtractPlugin({
      filename: "[name].bundle.css"
    })
  ],
  module: {
    rules: [
      {
        test: /\.scss$/,
        use: [
          MiniCssExtractPlugin.loader,
          "css-loader",
          {
            loader: "postcss-loader",
            options: {
              ident: "postcss",
              plugins: [AutoPrefixer()],
              sourceMap: true
            }
          },
          {
            loader: "resolve-url-loader"
          },
          {
            loader: "sass-loader",
            options: {
              precision: 8,
              includePaths: [],
              sourceMap: true
            }
          }
        ]
      }
    ]
  }
};

These entry files will have their variables set & customizations applied in each respective entry file, which looks like this: 这些条目文件将在每个相应的条目文件中应用其变量设置和自定义,如下所示:

// Entry file: modern_style.scss
$someVariableToBeUsedOrOverwritten: red;

@import "common/allModulesAreImportedHere"

.customRule{
   //...
}

This generated style, eg default.bundle.css is then loaded via <link rel="stylesheet" type="text/css" [href]="linkToTheme"> 然后,通过<link rel="stylesheet" type="text/css" [href]="linkToTheme">加载此生成的样式,例如default.bundle.css

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

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