Angular Material自定义主题使弹出窗口透明

时间:2018-09-19 15:27:23

标签: angular sass datepicker angular-material2

我目前正在使用Material2在Angular的仪表板上进行样式设计。我遇到一个问题,其中应用程序中的任何弹出窗口均以透明背景显示。

我在StackBlitz中举例说明了这个问题。

在堆叠闪电战中,当我添加custom material theme时出现了问题。

当我从项目"src/theme.scss"文件中删除angular.json时,像这样使用style.css中的预制材质主题;

@import '~@angular/material/prebuilt-themes/deeppurple-amber.css';

弹出窗口的背景再次正常。

所有弹出式窗口(日期选择器,分页器等)都会发生这种情况。

我的~/src/theme.scss文件看起来像这样;

@import '~@angular/material/theming';
// always include only once per project

@include mat-core();

// define 3 theme color
// mat-palette accepts $palette-name, main, lighter and darker variants
$dashboard-theme-primary: mat-palette($mat-blue);
$dashboard-theme-accent: mat-palette($mat-grey);
$dashboard-theme-warn: mat-palette($mat-red);

// create theme (use mat-dark-theme for themes with dark backgrounds)
$dashboard-theme: mat-light-theme(
    $dashboard-theme-primary,
    $dashboard-theme-accent,
    $dashboard-theme-warn
);

$dashboard-theme-dark: mat-dark-theme(
    $dashboard-theme-primary,
    $dashboard-theme-accent,
    $dashboard-theme-warn
);

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

.dashboard-theme-dark {
  @include angular-material-theme($dashboard-theme-dark);
}

主题已应用于<div>中的app.component.html上的项目

非常感谢您的帮助:)

更新

我已将整个项目更新为使用scss而不是css,然后重新构建了整个自定义theme.scss所有材料组件均已正确设置样式,可以在明暗之间动态切换主题,我什至可以设置非材质组件的样式。但是除非我使用如上所述的预构建主题,否则弹出窗口将保持透明。

为了确定我还更新了所有依赖项;

package.json

{
  "name": "DiagnosisDashboard",
  "version": "0.0.0",
  "scripts": {
    "ng": "ng",
    "start": "ng serve",
    "build": "ng build",
    "test": "ng test",
    "lint": "ng lint",
    "e2e": "ng e2e"
  },
  "private": true,
  "dependencies": {
    "@angular/animations": "^6.1.7",
    "@angular/cdk": "^6.4.7",
    "@angular/common": "^6.1.0",
    "@angular/compiler": "^6.1.0",
    "@angular/core": "^6.1.8",
    "@angular/forms": "^6.1.8",
    "@angular/http": "^6.1.8",
    "@angular/material": "^6.4.7",
    "@angular/material-moment-adapter": "^6.4.7",
    "@angular/platform-browser": "^6.1.8",
    "@angular/platform-browser-dynamic": "^6.1.8",
    "@angular/router": "^6.1.8",
    "core-js": "^2.5.4",
    "moment": "^2.22.2",
    "rxjs": "^6.0.0",
    "zone.js": "~0.8.26"
  },
  "devDependencies": {
    "@angular-devkit/build-angular": "~0.8.2",
    "@angular/cli": "^6.1.5",
    "@angular/compiler-cli": "^6.1.0",
    "@angular/language-service": "^6.1.8",
    "@types/jasmine": "~2.8.6",
    "@types/jasminewd2": "~2.0.3",
    "@types/node": "~10.10.1",
    "codelyzer": "~4.4.4",
    "jasmine-core": "~3.2.1",
    "jasmine-spec-reporter": "~4.2.1",
    "karma": "~3.0.0",
    "karma-chrome-launcher": "~2.2.0",
    "karma-coverage-istanbul-reporter": "^2.0.4",
    "karma-jasmine": "~1.1.1",
    "karma-jasmine-html-reporter": "^1.3.1",
    "protractor": "^5.4.1",
    "ts-node": "~7.0.1",
    "tslint": "~5.11.0",
    "typescript": "~2.7.2"
  }
}

angular.json

{
  "$schema": "./node_modules/@angular/cli/lib/config/schema.json",
  "version": 1,
  "newProjectRoot": "projects",
  "projects": {
    "my-app": {
      "root": "",
      "sourceRoot": "src",
      "projectType": "application",
      "prefix": "app",
      "schematics": {},
      "architect": {
        "build": {
          "builder": "@angular-devkit/build-angular:browser",
          "options": {
            "outputPath": "dist/my-app",
            "index": "src/index.html",
            "main": "src/main.ts",
            "polyfills": "src/polyfills.ts",
            "tsConfig": "src/tsconfig.app.json",
            "assets": [
              "src/favicon.ico",
              "src/assets"
            ],
            "styles": [
              "src/styles.scss",
              "src/app/theme.scss"
            ],
            "scripts": []
          },
          "configurations": {
            "production": {
              "fileReplacements": [
                {
                  "replace": "src/environments/environment.ts",
                  "with": "src/environments/environment.prod.ts"
                }
              ],
              "optimization": true,
              "outputHashing": "all",
              "sourceMap": false,
              "extractCss": true,
              "namedChunks": false,
              "aot": true,
              "extractLicenses": true,
              "vendorChunk": false,
              "buildOptimizer": true
            }
          }
        },
        "serve": {
          "builder": "@angular-devkit/build-angular:dev-server",
          "options": {
            "browserTarget": "my-app:build"
          },
          "configurations": {
            "production": {
              "browserTarget": "my-app:build:production"
            }
          }
        },
        "extract-i18n": {
          "builder": "@angular-devkit/build-angular:extract-i18n",
          "options": {
            "browserTarget": "my-app:build"
          }
        },
        "test": {
          "builder": "@angular-devkit/build-angular:karma",
          "options": {
            "main": "src/test.ts",
            "polyfills": "src/polyfills.ts",
            "tsConfig": "src/tsconfig.spec.json",
            "karmaConfig": "src/karma.conf.js",
            "styles": [
              "src/styles.scss",
              "src/app/theme.scss"
            ],
            "scripts": [],
            "assets": [
              "src/favicon.ico",
              "src/assets"
            ]
          }
        },
        "lint": {
          "builder": "@angular-devkit/build-angular:tslint",
          "options": {
            "tsConfig": [
              "src/tsconfig.app.json",
              "src/tsconfig.spec.json"
            ],
            "exclude": [
              "**/node_modules/**"
            ]
          }
        }
      }
    },
    "my-app-e2e": {
      "root": "e2e/",
      "projectType": "application",
      "architect": {
        "e2e": {
          "builder": "@angular-devkit/build-angular:protractor",
          "options": {
            "protractorConfig": "e2e/protractor.conf.js",
            "devServerTarget": "my-app:serve"
          },
          "configurations": {
            "production": {
              "devServerTarget": "my-app:serve:production"
            }
          }
        },
        "lint": {
          "builder": "@angular-devkit/build-angular:tslint",
          "options": {
            "tsConfig": "e2e/tsconfig.e2e.json",
            "exclude": [
              "**/node_modules/**"
            ]
          }
        }
      }
    }
  },
  "defaultProject": "my-app",
  "schematics": {
    "@schematics/angular:component": {
      "styleext": "scss"
    }
  }
}

新的StackBlitz

1 个答案:

答案 0 :(得分:1)

“ Popups”包含在覆盖容器中,该容器是<body>的直接子代。应用于基本应用程序组件的任何样式都不会应用于叠加层容器-您必须明确地应用它。请参阅theming docs的“多个主题和基于覆盖的组件”部分。例如:

import {OverlayContainer} from '@angular/cdk/overlay';

export class AppComponent {

    constructor(overlayContainer: OverlayContainer) { }

    applyTheme(theme: string) {
        this.overlayContainer.getContainerElement().classList.add(theme);
    }
}