可以在scss中插入角度环境变量吗? [英] It's possible to insert angular environment variable into scss?

查看:248
本文介绍了可以在scss中插入角度环境变量吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正试图找到一种方法来更改基于配置文件"变量环境的scss的编译方式. 我的应用程序将安装在不同的位置,主题不同,但是所有css都相同.我只需要在编译期间更改将使用的color palette. 我的应用程序在environments.ts上已经有一个profile变量,该变量将用于brand image.

I'm trying to find a way to change how my scss is compiled based on a "profile" variable environment. My app will be installed on different locations, witch with different theme, but all the css is the same. I just need to change what color palette will be used, during compilation. My app already have on the environments.ts a profile variable, that will be used for brand image.

我在想类似的东西

export const environment = {
  production: true,
  profile: 'appX'
  apiUrl: 'http://mydoainX.com/api/',
};

,然后在scss上:

$profile: environment.profile;

可以做这样的事情吗?

有什么想法吗?

推荐答案

最终找到了这个

Eventually a found this question, witch lead me to the solution.

它说要在.angular-cli.json中使用不同的.scss文件创建多个应用.

It says to create multiple apps with different .scss file in .angular-cli.json.

第1步: 为每个环境使用.scss文件创建几个文件夹,所有.scss文件都具有相同的文件名:

Step 1: Create several folders with .scss file for each environment, all the .scss files has the same filename:

|- src
  ...
  |- environments
    |- app1
      |- scss
        |- globalVars.scss
      |- environment.dev.ts
      |- environment.prod.ts
    |- app2
      |- scss
        |- globalVars.scss
      |- environment.dev.ts
      |- environment.prod.ts
  ...

src/environments/app1/scss/globalVars.scss中的

: $ profile:'app1'.

in src/environments/app1/scss/globalVars.scss: $profile: 'app1'.

: $ profile:'app2'.

in src/environments/app2/scss/globalVars.scss: $profile: 'app2'.

第2步:

.angular-cli.json中添加多个应用程序(Angular-cli Wiki在这里 ),并在每个应用程序对象中添加stylePreprocessorOptions条目,以用于 每个环境(此处为Angular-cli Wiki).

Add multiple apps in .angular-cli.json ( Angular-cli Wiki Here ), and add stylePreprocessorOptions entry in each app object for each environment ( Angular-cli Wiki Here ).

"apps": [
  {
    ...
    "name": "app1",
    "environments": {
      "dev": "environments/app1/environment.dev.ts",
      "prod": "environments/app1/environment.prod.ts"
    },
    "stylePreprocessorOptions": {
      "includePaths": [
        "environments/app1/scss"
      ]
    }
    ...
  },
  {
    ...
    "name": "app2",
    "environments": {
      "dev": "environments/app2/environment.dev.ts",
      "prod": "environments/app2/environment.prod.ts"
    },
    "stylePreprocessorOptions": {
      "includePaths": [
        "environments/app2/scss"
      ]
    }
    ...
  },
  ...
],

第3步:

globalVars.scss导入需要env特定的变量. 请勿使用相对路径!

Import the globalVars.scss where the env-specific variables needed. Do not use the relative path!

@import "globalVars";

使用ng build --app=app1时,$profile将为'app1',依此类推.

When using ng build --app=app1, the $profile will be 'app1'and so on.

第4步: 在我的theme.scss上,我使用了这样的变量:

Step 4: On my theme.scss i used the variable like this:

.my-style {
  @if $profile == 'app1' {@include angular-material-theme($theme-app1);}
  @if $profile == 'app2' {@include angular-material-theme($theme-app2);}

  &.contrast {
    @include angular-material-theme($theme-contrast);
  }
}

这篇关于可以在scss中插入角度环境变量吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆