Angular 8 和 Angular cli, "ng serve"包含 moment.js 或 popper.js 模块的应用程序问题

标签 angular angular-cli momentjs angular8 popper.js

我已经使用 Angular 8 和 cli 使用“ng new repro-app2”创建了一个应用程序。 问题出现时,在包含 moment.js 模块或 popper.js 模块后,我从我的电脑运行“ngserve”以在本地提供应用程序( 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

最佳答案

angular.json 文件中的 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);
  }
}

关于Angular 8 和 Angular cli, "ng serve"包含 moment.js 或 popper.js 模块的应用程序问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56872862/

相关文章:

Angular 7在构建后重新定位 Assets 文件

angular - 如何在发送之前动态更改 proxy.conf.js 中的请求

angular - 如何使用 angular-cli 启用 watch 进行构建?

html - 两行而不是单行的 Angular 重复

angular - 如何将 Angular 项目集成到 Squarespace 自定义模板中

momentjs - 不修改日期的时刻到ISOstring

javascript - 将德国日期转换为 ISO 日期

javascript - 如何在moment js中将纪元时间戳转换为带时区的日期时间?

angular - Angular2 的管道如何与数据绑定(bind)一起工作?

Angular 9 - 如何延长(区域设置感知周开始)NativeDateAdapter 工作?