最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Angular v19 Conditional Material Theme Palettes - Stack Overflow

programmeradmin0浏览0评论

I am developing an Angular v19 application and I am struggling to find a way to programatically change the material theme color palettes.

The reason for this feature is to make it very apparent when testing the application that we are not accidently using the production version. I am trying to read the current environment (production, development, testing) and set the color paette accordingly.

I first tried creating separate scss files for each scenario and hoped to use fileReplacements in angular.json to set a style file from which I could read a colour palette. It appears that fileReplacements doesn't allow .scss files.

I then tried using a SASS @if in the main styles.scss file to set the mat.theme conditionally on a css variable which could be changed programatically, but I can't seem to figure this out.

Reading the docs there may be a way to achieve this with $overrides.

If anyone knows how to switch themes, or color palettes programatically I would be very interested to learn.

I am developing an Angular v19 application and I am struggling to find a way to programatically change the material theme color palettes.

The reason for this feature is to make it very apparent when testing the application that we are not accidently using the production version. I am trying to read the current environment (production, development, testing) and set the color paette accordingly.

I first tried creating separate scss files for each scenario and hoped to use fileReplacements in angular.json to set a style file from which I could read a colour palette. It appears that fileReplacements doesn't allow .scss files.

I then tried using a SASS @if in the main styles.scss file to set the mat.theme conditionally on a css variable which could be changed programatically, but I can't seem to figure this out.

Reading the docs there may be a way to achieve this with $overrides.

If anyone knows how to switch themes, or color palettes programatically I would be very interested to learn.

Share Improve this question asked Feb 2 at 19:15 ChrisChris 1532 silver badges12 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 1

Production has its own styles array in angular.json, just add the style here and you can configure styles as per the environment:

      "configurations": {
        "production": {
          "styles": ["/src/light.scss"],
          "budgets": [
            {
              "type": "initial",
              "maximumWarning": "500kb",
              "maximumError": "1mb"
            },
            {
              "type": "anyComponentStyle",
              "maximumWarning": "2kb",
              "maximumError": "4kb"
            }
          ],
          "fileReplacements": [
            {
              "replace": "src/environments/environment.ts",
              "with": "src/environments/environment.prod.ts"
            }
          ],
          "outputHashing": "all"
        },
发布评论

评论列表(0)

  1. 暂无评论