【问题标题】:How to set HMR on ng build --watch and reload URL automatically?如何在 ng build --watch 上设置 HMR 并自动重新加载 URL?
【发布时间】:2021-06-26 11:10:40
【问题描述】:

我正在尝试在 ng build --watch 上设置 HOT RELOAD。我尝试了很多方法来放置手表。它也认得。但是,我无法用它重新加载 URL。我需要手动重新加载网址。

有人可以帮忙吗?

更多信息,我想在 Angular 项目的基本设置上实现。

npm install -g @angular/cli
ng new mydreamapp
cd my-dream-app
ng build --watch

这里是 angular.json 供参考

{
  "$schema": "./node_modules/@angular/cli/lib/config/schema.json",
  "version": 1,
  "newProjectRoot": "projects",
  "projects": {
    "mydreamapp": {
      "projectType": "application",
      "schematics": {},
      "root": "",
      "sourceRoot": "src",
      "prefix": "app",
      "architect": {
        "build": {
          "builder": "@angular-devkit/build-angular:browser",
          "options": {
            "outputPath": "dist/mydreamapp",
            "index": "src/index.html",
            "main": "src/main.ts",
            "polyfills": "src/polyfills.ts",
            "tsConfig": "tsconfig.app.json",
            "aot": true,
            "assets": [
              "src/favicon.ico",
              "src/assets"
            ],
            "styles": [
              "src/styles.css"
            ],
            "scripts": []
          },
          "configurations": {
            "production": {
              "fileReplacements": [
                {
                  "replace": "src/environments/environment.ts",
                  "with": "src/environments/environment.prod.ts"
                }
              ],
              "optimization": true,
              "outputHashing": "all",
              "sourceMap": false,
              "namedChunks": false,
              "extractLicenses": true,
              "vendorChunk": false,
              "buildOptimizer": true,
              "budgets": [
                {
                  "type": "initial",
                  "maximumWarning": "2mb",
                  "maximumError": "5mb"
                },
                {
                  "type": "anyComponentStyle",
                  "maximumWarning": "6kb",
                  "maximumError": "10kb"
                }
              ]
            }
          }
        },
        "serve": {
          "builder": "@angular-devkit/build-angular:dev-server",
          "options": {
            "browserTarget": "mydreamapp:build"
          },
          "configurations": {
            "production": {
              "browserTarget": "mydreamapp:build:production"
            }
          }
        },
        "extract-i18n": {
          "builder": "@angular-devkit/build-angular:extract-i18n",
          "options": {
            "browserTarget": "mydreamapp: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",
            "assets": [
              "src/favicon.ico",
              "src/assets"
            ],
            "styles": [
              "src/styles.css"
            ],
            "scripts": []
          }
        },
        "lint": {
          "builder": "@angular-devkit/build-angular:tslint",
          "options": {
            "tsConfig": [
              "tsconfig.app.json",
              "tsconfig.spec.json",
              "e2e/tsconfig.json"
            ],
            "exclude": [
              "**/node_modules/**"
            ]
          }
        },
        "e2e": {
          "builder": "@angular-devkit/build-angular:protractor",
          "options": {
            "protractorConfig": "e2e/protractor.conf.js",
            "devServerTarget": "mydreamapp:serve"
          },
          "configurations": {
            "production": {
              "devServerTarget": "mydreamapp:serve:production"
            }
          }
        }
      }
    }
  },
  "defaultProject": "mydreamapp"
}

【问题讨论】:

  • 您能否将您的 main.ts 和其他配置代码更新为有助于发现问题的问题
  • @ZamAbdulVahid 我已经更新了它。请看一下
  • angular.json 中 options 数组下 'outputPath' 的值是什么
  • @ZamAbdulVahid 请检查 angular.json

标签: angular webpack angular-cli webpack-dev-server


【解决方案1】:

很难从 Angular 文档中找到我们需要在 watch 命令中添加输出路径。

这个帖子讨论了同样的问题:https://github.com/angular/angular-cli/issues/2511

由于您在 angular.json 中提到的输出文件夹是“dist”,请使用以下命令。

ng build --output-path dist --watch

您可以在 package json 的 script 部分下添加相同的命令,以方便使用。

【讨论】:

  • 它对我不起作用。如何在本地查看?
猜你喜欢
  • 2018-06-15
  • 1970-01-01
  • 2016-07-21
  • 2020-03-19
  • 1970-01-01
  • 2016-11-15
  • 1970-01-01
  • 2019-04-23
  • 1970-01-01
相关资源
最近更新 更多