Angular 7 去除 SCSS 中的重複屬性
P粉680487967
P粉680487967 2024-04-04 23:28:54
0
1
1491

Angular SASS (.scss) 剝離屬性。如何防止這種情況發生,以便我可以嵌入重複的屬性以實現跨瀏覽器相容性?

要複製,請建立一個新的 Angular 7.2 專案

{
      "name": "default-app",
      "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": "~7.2.0",
        "@angular/common": "~7.2.0",
        "@angular/compiler": "~7.2.0",
        "@angular/core": "~7.2.0",
        "@angular/forms": "~7.2.0",
        "@angular/platform-browser": "~7.2.0",
        "@angular/platform-browser-dynamic": "~7.2.0",
        "@angular/router": "~7.2.0",
        "core-js": "^2.5.4",
        "rxjs": "~6.3.3",
        "tslib": "^1.9.0",
        "zone.js": "~0.8.26"
      },
      "devDependencies": {
        "@angular-devkit/build-angular": "~0.13.0",
        "@angular/cli": "~7.3.9",
        "@angular/compiler-cli": "~7.2.0",
        "@angular/language-service": "~7.2.0",
        "@types/node": "~8.9.4",
        "@types/jasmine": "~2.8.8",
        "@types/jasminewd2": "~2.0.3",
        "codelyzer": "~4.5.0",
        "jasmine-core": "~2.99.1",
        "jasmine-spec-reporter": "~4.2.1",
        "karma": "~4.0.0",
        "karma-chrome-launcher": "~2.2.0",
        "karma-coverage-istanbul-reporter": "~2.0.1",
        "karma-jasmine": "~1.1.2",
        "karma-jasmine-html-reporter": "^0.2.2",
        "protractor": "~5.4.0",
        "ts-node": "~7.0.0",
        "tslint": "~5.11.0",
        "typescript": "~3.2.2"
      }
    }

將「teststyle」類別加入到元件中的任何元素。

<div style="text-align:center" class="teststyle">Hello
</div>
<router-outlet></router-outlet>

在元件對應的.scss檔中套用

.teststyle
{
    background: #a64bf4;
    background: -webkit-linear-gradient(right, #6ab443, #04a95f, #00d421);
    background: -o-linear-gradient(right, #6ab443, #04a95f, #00d421);
    background: -moz-linear-gradient(right, #6ab443, #04a95f, #00d421);
    background: linear-gradient(right, #6ab443, #04a95f, #00d421);
    
}

角度預處理器會刪除除第一個屬性和最後一個屬性之外的所有內容。 對應的css是這樣的。 -webkit-線性梯度、-o-線性梯度和-moz-線性梯度不是轉譯的css/js檔。這是一個 tarnspilation 問題,而不是瀏覽器問題。

當我使用直接 css 時,我看到了這一點,這是正確的。

還包括 angular.json

{
  "$schema": "./node_modules/@angular/cli/lib/config/schema.json",
  "version": 1,
  "newProjectRoot": "projects",
  "projects": {
    "default-app": {
      "root": "",
      "sourceRoot": "src",
      "projectType": "application",
      "prefix": "app",
      "schematics": {
        "@schematics/angular:component": {
          "style": "scss"
        }
      },
      "architect": {
        "build": {
          "builder": "@angular-devkit/build-angular:browser",
          "options": {
            "outputPath": "dist/default-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"
            ],
            "scripts": [],
            "es5BrowserSupport": true
          },
          "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,
              "budgets": [
                {
                  "type": "initial",
                  "maximumWarning": "2mb",
                  "maximumError": "5mb"
                }
              ]
            }
          }
        },
        "serve": {
          "builder": "@angular-devkit/build-angular:dev-server",
          "options": {
            "browserTarget": "default-app:build"
          },
          "configurations": {
            "production": {
              "browserTarget": "default-app:build:production"
            }
          }
        },
        "extract-i18n": {
          "builder": "@angular-devkit/build-angular:extract-i18n",
          "options": {
            "browserTarget": "default-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"
            ],
            "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/**"
            ]
          }
        }
      }
    },
    "default-app-e2e": {
      "root": "e2e/",
      "projectType": "application",
      "prefix": "",
      "architect": {
        "e2e": {
          "builder": "@angular-devkit/build-angular:protractor",
          "options": {
            "protractorConfig": "e2e/protractor.conf.js",
            "devServerTarget": "default-app:serve"
          },
          "configurations": {
            "production": {
              "devServerTarget": "default-app:serve:production"
            }
          }
        },
        "lint": {
          "builder": "@angular-devkit/build-angular:tslint",
          "options": {
            "tsConfig": "e2e/tsconfig.e2e.json",
            "exclude": [
              "**/node_modules/**"
            ]
          }
        }
      }
    }
  },
  "defaultProject": "default-app"
}

P粉680487967
P粉680487967

全部回覆(1)
P粉482108310

Angular 使用 Autoprefixer 新增任何所需的供應商前綴並刪除不需要的前綴。基本上您無需擔心添加任何內容,因為它們會為您添加。被刪除的那些是因為不需要它們。

查看:https://caniuse.com/?search=linear-gradient 使用與支援

  • -webkit-linear-gradient 只需要

    • Chrome 4-25(2010-2013 年發布)- 全球 0% 瀏覽器
    • Safari 4-6.1(2010-2013 年發布)- 全球瀏覽器佔比 0.01%
    • iOS Safari 3.2-6.1(2010-2013 年發布)- 全球瀏覽器的 0.01%
    • Android 瀏覽器 2.1-4.3(2009-2013 年發布) - 佔全球瀏覽器的 0.13%
  • -o-linear-gradient 僅 Opera 11.5 和 Opera mobile 12(2011-2012 發布)需要,全球瀏覽器的比例為 0%

  • -moz-linear-gradient 僅 Firefox 3.5-15(2010-2012 發布)需要,佔瀏覽器的 0.01%


如果您仍然希望/需要在2018 年推出的Angular 版本中使用0% 使用10 多年的舊瀏覽器支援CSS 漸變,您只需新增一個src/browserslist 檔案( 相關問題)您可以在此處了解該文件格式

#
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板