角。与本地化构建时,全球样式不适用

发布于 2025-01-30 04:47:19 字数 5591 浏览 4 评论 0原文

我将本地化添加到我的Angular V12.2.7应用程序中,并设置本地化= True。 我运行命令来构建应用程序。它将应用程序构建到3个文件夹(EN,UK,RU)。

npm run build -- --optimization=false --configuration=staging

我设置了我的nginx.config,以便它使用所需语言返回应用程序,

location ~ ^/(en|ru|uk) {
    try_files $uri /$1/index.html?$args;
}

但事实证明,当构建本地化应用程序时,全局样式(来自src/styles.scss)不适用。 样式正确包含在Angular.json:

  "$schema": "./node_modules/@angular/cli/lib/config/schema.json",
  "version": 1,
  "newProjectRoot": "projects",
  "projects": {
    "BooTravel": {
      "projectType": "application",
      "schematics": {
        "@schematics/angular:component": {
          "style": "scss"
        },
        "@schematics/angular:application": {
          "strict": true
        }
      },
      "root": "",
      "sourceRoot": "src",
      "prefix": "app",
      "i18n": {
        "sourceLocale": "en",
        "locales": {
          "uk": {
            "translation": "src/i18n/messages.uk.xlf",
            "baseHref": "/uk/"
          },
          "ru": {
            "translation": "src/i18n/messages.ru.xlf",
            "baseHref": "/ru/"
          }
        }
      },
      "architect": {
        "build": {
          "builder": "@angular-devkit/build-angular:browser",
          "options": {
            "localize": true,
            "outputPath": "dist/",
            "index": "src/index.html",
            "main": "src/main.ts",
            "polyfills": "src/polyfills.ts",
            "tsConfig": "tsconfig.app.json",
            "inlineStyleLanguage": "scss",
            "assets": [
              "src/favicon.ico",
              "src/assets"
            ],
            "styles": [
              "src/styles.scss"
            ],
            "scripts": []
          },
          "configurations": {
            "production": {
              "budgets": [
                {
                  "type": "initial",
                  "maximumWarning": "4mb",
                  "maximumError": "6mb"
                },
                {
                  "type": "anyComponentStyle",
                  "maximumWarning": "2kb",
                  "maximumError": "4kb"
                }
              ],
              "fileReplacements": [
                {
                  "replace": "src/environments/environment.ts",
                  "with": "src/environments/environment.prod.ts"
                }
              ],
              "outputHashing": "all"
            },
            "preprod": {
              "fileReplacements": [
                {
                  "replace": "src/environments/environment.ts",
                  "with": "src/environments/environment.preprod.ts"
                }
              ]
            },
            "staging": {
              "fileReplacements": [
                {
                  "replace": "src/environments/environment.ts",
                  "with": "src/environments/environment.staging.ts"
                }
              ]
            },
            "localhost": {
              "fileReplacements": [
                {
                  "replace": "src/environments/environment.ts",
                  "with": "src/environments/environment.localhost.ts"
                }
              ]
            },
            "uk": {
              "localize": ["uk"]
            },
            "ru": {
              "localize": ["ru"]
            },
            "development": {
              "buildOptimizer": false,
              "optimization": false,
              "vendorChunk": true,
              "extractLicenses": false,
              "sourceMap": true,
              "namedChunks": true
            }
          },
          "defaultConfiguration": "production"
        },
        "serve": {
          "builder": "@angular-devkit/build-angular:dev-server",
          "configurations": {
            "production": {
              "browserTarget": "BooTravel:build:production"
            },
            "preprod": {
              "browserTarget": "inQuestUI:build:preprod"
            },
            "staging": {
              "browserTarget": "inQuestUI:build:staging"
            },
            "localhost": {
              "browserTarget": "inQuestUI:build:localhost"
            },
            "development": {
              "browserTarget": "BooTravel:build:development"
            },
            "uk": {
              "browserTarget": "BooTravel:build:uk"
            },
            "ru": {
              "browserTarget": "BooTravel:build:ru"
            }
          },
          "defaultConfiguration": "development"
        },
        "extract-i18n": {
          "builder": "@angular-devkit/build-angular:extract-i18n",
          "options": {
            "browserTarget": "BooTravel:build"
          }
        },
        "test": {
          "builder": "@angular-devkit/build-angular:karma",
          "options": {
            "main": "src/test.ts",
            "polyfills": "src/polyfills.ts",
            "tsConfig": "tsconfig.spec.json",
            "karmaConfig": "karma.conf.js",
            "inlineStyleLanguage": "scss",
            "assets": [
              "src/favicon.ico",
              "src/assets"
            ],
            "styles": [
              "src/styles.scss"
            ],
            "scripts": []
          }
        }
      }
    }
  },
  "defaultProject": "BooTravel"

已解决: 事实证明,nginx将CSS文件作为文本/普通 在此处找到该解决方案(从NGINX删除HTTP部分): 防止nginx将CSS文件作为文本/plain plain

I added localization to my Angular v12.2.7 app and set localize=true.
I run the command to build the app. And it builds the app to 3 folders (en, uk, ru).

npm run build -- --optimization=false --configuration=staging

I set up my nginx.config so that it returns the app with the needed language

location ~ ^/(en|ru|uk) {
    try_files $uri /$1/index.html?$args;
}

Everything works fine but it turned out that global styles (from src/styles.scss) do not apply when building the localized app.
Styles are correctly included in angular.json:

  "$schema": "./node_modules/@angular/cli/lib/config/schema.json",
  "version": 1,
  "newProjectRoot": "projects",
  "projects": {
    "BooTravel": {
      "projectType": "application",
      "schematics": {
        "@schematics/angular:component": {
          "style": "scss"
        },
        "@schematics/angular:application": {
          "strict": true
        }
      },
      "root": "",
      "sourceRoot": "src",
      "prefix": "app",
      "i18n": {
        "sourceLocale": "en",
        "locales": {
          "uk": {
            "translation": "src/i18n/messages.uk.xlf",
            "baseHref": "/uk/"
          },
          "ru": {
            "translation": "src/i18n/messages.ru.xlf",
            "baseHref": "/ru/"
          }
        }
      },
      "architect": {
        "build": {
          "builder": "@angular-devkit/build-angular:browser",
          "options": {
            "localize": true,
            "outputPath": "dist/",
            "index": "src/index.html",
            "main": "src/main.ts",
            "polyfills": "src/polyfills.ts",
            "tsConfig": "tsconfig.app.json",
            "inlineStyleLanguage": "scss",
            "assets": [
              "src/favicon.ico",
              "src/assets"
            ],
            "styles": [
              "src/styles.scss"
            ],
            "scripts": []
          },
          "configurations": {
            "production": {
              "budgets": [
                {
                  "type": "initial",
                  "maximumWarning": "4mb",
                  "maximumError": "6mb"
                },
                {
                  "type": "anyComponentStyle",
                  "maximumWarning": "2kb",
                  "maximumError": "4kb"
                }
              ],
              "fileReplacements": [
                {
                  "replace": "src/environments/environment.ts",
                  "with": "src/environments/environment.prod.ts"
                }
              ],
              "outputHashing": "all"
            },
            "preprod": {
              "fileReplacements": [
                {
                  "replace": "src/environments/environment.ts",
                  "with": "src/environments/environment.preprod.ts"
                }
              ]
            },
            "staging": {
              "fileReplacements": [
                {
                  "replace": "src/environments/environment.ts",
                  "with": "src/environments/environment.staging.ts"
                }
              ]
            },
            "localhost": {
              "fileReplacements": [
                {
                  "replace": "src/environments/environment.ts",
                  "with": "src/environments/environment.localhost.ts"
                }
              ]
            },
            "uk": {
              "localize": ["uk"]
            },
            "ru": {
              "localize": ["ru"]
            },
            "development": {
              "buildOptimizer": false,
              "optimization": false,
              "vendorChunk": true,
              "extractLicenses": false,
              "sourceMap": true,
              "namedChunks": true
            }
          },
          "defaultConfiguration": "production"
        },
        "serve": {
          "builder": "@angular-devkit/build-angular:dev-server",
          "configurations": {
            "production": {
              "browserTarget": "BooTravel:build:production"
            },
            "preprod": {
              "browserTarget": "inQuestUI:build:preprod"
            },
            "staging": {
              "browserTarget": "inQuestUI:build:staging"
            },
            "localhost": {
              "browserTarget": "inQuestUI:build:localhost"
            },
            "development": {
              "browserTarget": "BooTravel:build:development"
            },
            "uk": {
              "browserTarget": "BooTravel:build:uk"
            },
            "ru": {
              "browserTarget": "BooTravel:build:ru"
            }
          },
          "defaultConfiguration": "development"
        },
        "extract-i18n": {
          "builder": "@angular-devkit/build-angular:extract-i18n",
          "options": {
            "browserTarget": "BooTravel:build"
          }
        },
        "test": {
          "builder": "@angular-devkit/build-angular:karma",
          "options": {
            "main": "src/test.ts",
            "polyfills": "src/polyfills.ts",
            "tsConfig": "tsconfig.spec.json",
            "karmaConfig": "karma.conf.js",
            "inlineStyleLanguage": "scss",
            "assets": [
              "src/favicon.ico",
              "src/assets"
            ],
            "styles": [
              "src/styles.scss"
            ],
            "scripts": []
          }
        }
      }
    }
  },
  "defaultProject": "BooTravel"

solved:
It turned out that nginx served css files as text/plain
The solution was found here (removing http section from nginx):
Prevent nginx from serving css files as text/plain

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文