Getting ERROR ReferenceError: userAgent is not defined, in angular 4 production build (Angular 4)

Getting ERROR ReferenceError: userAgent is not defined, in angular 4 production build (Angular 4)

您好,我正在 angular 4 中开发一个项目,它在本地和开发环境中运行良好。但是当我进行产品构建时,生成了构建。但是前端控制台出现了一些错误。

以下是配置详情:

 "apps": [
    {
      "root": "src",
      "outDir": "dist",
      "assets": [
        "assets",
        "favicon.ico"
      ],
      "index": "index.html",
      "main": "main.ts",
      "polyfills": "polyfills.ts",
      "test": "test.ts",
      "tsconfig": "tsconfig.app.json",
      "testTsconfig": "tsconfig.spec.json",
      "prefix": "app",
      "styles": [

        "../node_modules/bootstrap/scss/bootstrap.scss",
        "../node_modules/font-awesome/css/font-awesome.css",
        "../node_modules/izimodal/css/iziModal.min.css",

        "../node_modules/flatpickr/dist/flatpickr.css",
        "assets/bootstrap-daterangepicker-master/daterangepicker.scss",
       "assets/css/style.css",
        "assets/css/common.css",
        "app.scss",
        "assets/css/select2.min.css"
      ],
      "scripts": [
        "../node_modules/jquery/dist/jquery.min.js",
        "assets/js/codebird.js",
        "../node_modules/popper.js/dist/umd/popper.min.js",
        "assets/bootstrap-daterangepicker-master/moment.js",
        "../node_modules/bootstrap/dist/js/bootstrap.min.js",
        "assets/js/flexibility.js",
        "../node_modules/flatpickr/dist/flatpickr.js",
        "../node_modules/izimodal/js/iziModal.min.js",
        "assets/bootstrap-daterangepicker-master/daterangepicker.js",
        "assets/js/select2.full.min.js"
      ],
      "environmentSource": "environments/environment.ts",
      "environments": {
        "dev": "environments/environment.ts",
        "staging": "environments/environment.staging.ts",
        "prod": "environments/environment.prod.ts",
        "uat": "environments/environment.uat.ts",
        "local": "environments/environment.local.ts",
        "develop": "environments/environment.dev.ts"
      }
    }
  ],
  "e2e": {
    "protractor": {
      "config": "./protractor.conf.js"
    }
  },
  "lint": [
    {
      "project": "src/tsconfig.app.json"
    },
    {
      "project": "src/tsconfig.spec.json"
    },
    {
      "project": "e2e/tsconfig.e2e.json"
    }
  ],
  "test": {
    "karma": {
      "config": "./karma.conf.js"
    }
  },
  "defaults": {
    "styleExt": "scss",
    "component": {
    }
  }

我遇到以下错误:

错误引用错误:未定义 userAgent。

我已经检查了使用脚本的文件,它在 codebird.js 中使用。 但无法解决问题。

看起来 izimodel 定义了一个变量 userAgent 及其错误原因。

izimodel.min.js 的副本放入您的 js 文件夹 并使用它代替 中的一个node_modules.

然后替换

(userAgent=navigator.userAgent,userAgent.indexOf("MSIE ")>-1||userAgent.indexOf("Trident/")>-1)

(navigator.userAgent.indexOf("MSIE ")>-1||navigator.userAgent.indexOf("Trident/")>-1)

那就解决问题了