【问题标题】:Angular 8 and Angular cli, "ng serve" problem with application including moment.js or popper.js modulesAngular 8 和 Angular cli,应用程序的“ng serve”问题,包括 moment.js 或 popper.js 模块
【发布时间】:2019-07-03 14:52:51
【问题描述】:

我创建了一个带有 angular 8 的应用程序和带有 "ng new repro-app2" 的 cli。 问题出现时,在包含 moment.js 模块或 popper.js 模块后,我从我的电脑运行“ng serve”以在本地提供应用程序 (http://localhost:4200/)。 浏览器的控制台(firefox 和 chrome)抛出这个错误:

moment.min.js:1 Uncaught TypeError: Cannot set property 'moment' of undefined
    at moment.min.js:1
    at moment.min.js:1

当我构建运行“ng build --output-hashing=all”的相同应用程序并将结果部署到我的网络服务器时,错误消失。

我的 angular.json 文件:

{
  "$schema": "./node_modules/@angular/cli/lib/config/schema.json",
  "version": 1,
  "newProjectRoot": "projects",
  "projects": {
    "repro-app": {
      "root": "",
      "sourceRoot": "src",
      "projectType": "application",
      "prefix": "app",
      "schematics": {},
      "architect": {
        "build": {
          "builder": "@angular-devkit/build-angular:browser",
          "options": {
            "outputPath": "dist/repro-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.css"
            ],
            "scripts": [
                "node_modules/moment/min/moment.min.js"
            ],
            "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": "repro-app:build"
          },
          "configurations": {
            "production": {
              "browserTarget": "repro-app:build:production"
            }
          }
        },
        "extract-i18n": {
          "builder": "@angular-devkit/build-angular:extract-i18n",
          "options": {
            "browserTarget": "repro-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.css"
            ],
            "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/**"
            ]
          }
        }
      }
    },
    "repro-app-e2e": {
      "root": "e2e/",
      "projectType": "application",
      "prefix": "",
      "architect": {
        "e2e": {
          "builder": "@angular-devkit/build-angular:protractor",
          "options": {
            "protractorConfig": "e2e/protractor.conf.js",
            "devServerTarget": "repro-app:serve"
          },
          "configurations": {
            "production": {
              "devServerTarget": "repro-app:serve:production"
            }
          }
        },
        "lint": {
          "builder": "@angular-devkit/build-angular:tslint",
          "options": {
            "tsConfig": "e2e/tsconfig.e2e.json",
            "exclude": [
              "**/node_modules/**"
            ]
          }
        }
      }
    }
  },
  "defaultProject": "repro-app"
}

我的 package.json 文件:

{
  "name": "repro-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": "~8.1.0",
    "@angular/common": "~8.1.0",
    "@angular/compiler": "~8.1.0",
    "@angular/core": "~8.1.0",
    "@angular/forms": "~8.1.0",
    "@angular/platform-browser": "~8.1.0",
    "@angular/platform-browser-dynamic": "~8.1.0",
    "@angular/router": "~8.1.0",
    "core-js": "^2.5.4",
    "moment": "^2.24.0",
    "rxjs": "~6.5.2",
    "tslib": "^1.9.0",
    "zone.js": "~0.9.1"
  },
  "devDependencies": {
    "@angular-devkit/build-angular": "~0.801.0",
    "@angular/cli": "~8.1.0",
    "@angular/compiler-cli": "~8.2.0-next.0",
    "@angular/language-service": "~8.1.0",
    "@types/node": "~8.9.4",
    "@types/jasmine": "~2.8.8",
    "@types/jasminewd2": "~2.0.3",
    "codelyzer": "^5.0.1",
    "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.4.5"
  }
}

可以从这里下载应用程序:

repro-app2

【问题讨论】:

  • "node_modules/popper.js/dist/umd/popper.min.js",
  • 您好,感谢您的回答。在这个 repro-app 中,我不使用 popper,而只使用 moment.js...
  • angular-cli 使用 webpack,查看 momentjs 的 webpack 配置

标签: angular angular-cli momentjs angular8 popper.js


【解决方案1】:

scripts 文件中的scripts 数组中删除"node_modules/moment/min/moment.min.js"

错误将消失。

因为你已经使用 npm 安装了 moment.js,所以你想在任何地方使用它,只需在你的组件中按以下方式导入它:

// app.component.ts
import { Component } from '@angular/core';
import * as moment from 'moment';

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})
export class AppComponent {
  title = 'repro-app';

  currentDate = moment(new Date()).format('MM/DD/YYYY')

  constructor() {
    console.log(this.currentDate);
  }
}

【讨论】:

  • 谢谢,这解决了问题。但有任何线索说明为什么会发生这种情况?
【解决方案2】:

关于 popper.js。当我将应用程序更新到 Angular 的 8 版本时也是同样的问题。作为一种解决方案,您可以使用 Bootstrap 缩小的捆绑 js 文件,它已经有 popper.js。

需要更改 angular.json 的 architect.build.options 部分中的 scripts 数组。最终会是这样的:

"scripts": [
    "./node_modules/jquery/dist/jquery.min.js",
    "./node_modules/bootstrap/dist/js/bootstrap.bundle.min.js",
],

希望,帮助你:)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-05-03
    • 2019-12-08
    • 2020-05-22
    • 1970-01-01
    • 1970-01-01
    • 2020-04-19
    • 2018-06-28
    相关资源
    最近更新 更多