【问题标题】:ReferenceError: MouseEvent is not definedReferenceError:未定义 MouseEvent
【发布时间】:2017-01-02 12:55:01
【问题描述】:

我正在尝试在使用 angular/universal-starter (TypeScript 2.x) 作为主干的项目中使用 ng2-select。

(我尝试在 angular-cli 生成的项目中添加 ng2-select,但效果很好)

在添加这两行之后:

import { SELECT_DIRECTIVES } from 'ng2-select/ng2-select';

@Component({
  directives: [SELECT_DIRECTIVES],
  ...

我的终端中已经出现错误:

ReferenceError: MouseEvent is not defined
    at /my-project/node_modules/ng2-select/components/select/off-click.js:33:42
    at Object.<anonymous> (/my-project/node_modules/ng2-select/components/select/off-click.js:43:2)
    at Module._compile (module.js:541:32)
    at Object.Module._extensions..js (module.js:550:10)
    at Module.load (module.js:458:32)
    at tryModuleLoad (module.js:417:12)
    at Function.Module._load (module.js:409:3)
    at Module.require (module.js:468:17)
    at require (internal/module.js:20:19)
    at Object.<anonymous> (/my-project/node_modules/ng2-select/components/select/select.js:20:19)
    at Module._compile (module.js:541:32)
    at Object.Module._extensions..js (module.js:550:10)
    at Module.load (module.js:458:32)
    at tryModuleLoad (module.js:417:12)
    at Function.Module._load (module.js:409:3)
    at Module.require (module.js:468:17)
    at require (internal/module.js:20:19)
    at Object.<anonymous> (/my-project/node_modules/ng2-select/components/select.js:2:16)
    at Module._compile (module.js:541:32)
    at Object.Module._extensions..js (module.js:550:10)
    at Module.load (module.js:458:32)
    at tryModuleLoad (module.js:417:12)
[nodemon] app crashed - waiting for file changes before starting...

这是我的 tsconfig.json 文件:

{
  "compilerOptions": {
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "target": "es5",
    "module": "commonjs",
    "removeComments": true,
    "sourceMap": true,
    "lib": ["es6", "dom"]
  },
  "include": [
    "node_modules/@types/**/*.d.ts",
    "src/**/*.ts"
  ],
  "exclude": [
    "node_modules",
    "!node_modules/@types/**/*.d.ts"
  ],
  "compileOnSave": false,
  "buildOnSave": false,
  "atom": {
    "rewriteTsconfig": false
  }
}

【问题讨论】:

    标签: angular typescript webpack typescript2.0 angular2-universal


    【解决方案1】:

    我想这个错误是因为指令没有正确加载造成的。

    1) 确保正在导入正确的组件,并验证是否定义了 MouseEvent

    2) 验证是否所有组件都在导出;

    3) 在

    @Component({
      directives: [SELECT_DIRECTIVES],
      ...
    

    确保SELECT_DIRECTIVES 不是数组对象。如果是,我认为您应该尝试使用

    [ ...SELECT_DIRECTIVES ]

    【讨论】:

    • 谢谢,但还是同样的错误。请注意,MouseEvent 位于 node_modules 文件夹中。它在我的 angular-cli 生成的项目中运行良好。我猜它与 tsconfig.json 有某种关系
    【解决方案2】:

    【讨论】:

      猜你喜欢
      • 2020-02-15
      • 2012-08-24
      • 2015-10-04
      • 2017-04-10
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多