简体   繁体   English

Angular:找不到环境文件

[英]Angular: Cannot find environment file

I followed several tutorials and they all say at some point "now go to src/environments ".我遵循了几个教程,他们都在某个时候说“现在 go 到src/environments ”。 But that folder just does not exist in my project, I tried to delete the whole project and recreate it.但是那个文件夹在我的项目中不存在,我试图删除整个项目并重新创建它。 but it doesn't help.但它没有帮助。

This is how I do it.我就是这样做的。 Open cmd and then:打开 cmd 然后:

1. cd my_path
2. npm install -g @angular/cli
3. ng new webversion
   --> Angular routing? Yes
   --> Stylesheet: CSS
4. cd webversion
5. npm install bootstrap
6. npm install firebase @angular/fire

Now the tutorials start using src/environments .现在教程开始使用src/environments But I just do not have that folder.但我只是没有那个文件夹。 This is my angular.json :这是我的angular.json

{
  "$schema": "./node_modules/@angular/cli/lib/config/schema.json",
  "version": 1,
  "newProjectRoot": "projects",
  "projects": {
    "webversion": {
      "projectType": "application",
      "schematics": {},
      "root": "",
      "sourceRoot": "src",
      "prefix": "app",
      "architect": {
        "build": {
          "builder": "@angular-devkit/build-angular:browser",
          "options": {
            "outputPath": "dist/webversion",
            "index": "src/index.html",
            "main": "src/main.ts",
            "polyfills": [
              "zone.js"
            ],
            "tsConfig": "tsconfig.app.json",
            "assets": [
              "src/favicon.ico",
              "src/assets"
            ],
            "styles": [
              "src/styles.css"
            ],
            "scripts": []
          },
          "configurations": {
            "production": {
              "budgets": [
                {
                  "type": "initial",
                  "maximumWarning": "500kb",
                  "maximumError": "1mb"
                },
                {
                  "type": "anyComponentStyle",
                  "maximumWarning": "2kb",
                  "maximumError": "4kb"
                }
              ],
              "outputHashing": "all"
            },
            "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": "webversion:build:production"
            },
            "development": {
              "browserTarget": "webversion:build:development"
            }
          },
          "defaultConfiguration": "development"
        },
        "extract-i18n": {
          "builder": "@angular-devkit/build-angular:extract-i18n",
          "options": {
            "browserTarget": "webversion:build"
          }
        },
        "test": {
          "builder": "@angular-devkit/build-angular:karma",
          "options": {
            "polyfills": [
              "zone.js",
              "zone.js/testing"
            ],
            "tsConfig": "tsconfig.spec.json",
            "assets": [
              "src/favicon.ico",
              "src/assets"
            ],
            "styles": [
              "src/styles.css"
            ],
            "scripts": []
          }
        }
      }
    }
  }
}

According to the tutorials tt should contain a "reference" to the environment folder, and a file replacement info about which files are going to be replaced in production mode.根据教程,tt 应该包含对环境文件夹的“引用”,以及关于哪些文件将在生产模式下被替换的文件替换信息。 But it doesn't have either of that...但这两者都没有...

What am I missing?我错过了什么? I followed multiple tutorials, multiple times each, step by step... I'm wasting hours already on this environment thing... Thanks a lot!我遵循了多个教程,每个教程多次,一步一步......我已经在这个环境上浪费时间了......非常感谢!

You'll have to create your environment files manually.您必须手动创建环境文件。

See Configure Environment Specific Defaults请参见配置环境特定默认值

This was removed from Angular 15 CLI in order to simplify the minimal app created via ng new in the new standalone world.这已从 Angular 15 CLI 中删除,以简化在新的独立世界中通过ng new创建的最小应用程序。 The reason they did this is that it should be really simple to create and bootstrap a single component with minimal additional files.他们这样做的原因是用最少的附加文件创建和引导单个组件应该非常简单。 It does mean older tutorials may be out of date.这确实意味着较旧的教程可能已过时。

  • Create environments directory创建环境目录

  • Create your custom environments创建您的自定义环境

    • environment.ts环境.ts
    • environment.prod.ts环境.prod.ts
    • environment.staging.ts etc. environment.staging.ts 等

angular.json - see <---- angular.json - 见<----

{
  "$schema": "./node_modules/@angular/cli/lib/config/schema.json",
  "version": 1,
  "newProjectRoot": "projects",
  "projects": {
    "webversion": {
      "projectType": "application",
      "schematics": {},
      "root": "",
      "sourceRoot": "src",
      "prefix": "app",
      "architect": {
        "build": {
          "builder": "@angular-devkit/build-angular:browser",
          "options": {
            "outputPath": "dist/webversion",
            "index": "src/index.html",
            "main": "src/main.ts",
            "polyfills": [
              "zone.js"
            ],
            "tsConfig": "tsconfig.app.json",
            "assets": [
              "src/favicon.ico",
              "src/assets"
            ],
            "styles": [
              "src/styles.css"
            ],
            "scripts": []
          },
          "configurations": {
            "production": {
              "budgets": [
                {
                  "type": "initial",
                  "maximumWarning": "500kb",
                  "maximumError": "1mb"
                },
                {
                  "type": "anyComponentStyle",
                  "maximumWarning": "2kb",
                  "maximumError": "4kb"
                }
              ],
              "outputHashing": "all",
              "fileReplacements": [ // <------------------------------------
                {
                  "replace": "src/environments/environment.ts",
                  "with": "src/environments/environment.prod.ts"
                }
              ],
            },
            "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": "webversion:build:production"
            },
            "development": {
              "browserTarget": "webversion:build:development"
            }
          },
          "defaultConfiguration": "development"
        },
        "extract-i18n": {
          "builder": "@angular-devkit/build-angular:extract-i18n",
          "options": {
            "browserTarget": "webversion:build"
          }
        },
        "test": {
          "builder": "@angular-devkit/build-angular:karma",
          "options": {
            "polyfills": [
              "zone.js",
              "zone.js/testing"
            ],
            "tsConfig": "tsconfig.spec.json",
            "assets": [
              "src/favicon.ico",
              "src/assets"
            ],
            "styles": [
              "src/styles.css"
            ],
            "scripts": []
          }
        }
      }
    }
  }
}

If you need additional configurations, duplicate the production one and rename.如果您需要其他配置,请复制生产配置并重命名。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM