1

我試圖在使用angular/universal-starter(TypeScript 2.x)作爲主幹的項目中使用ng2-selectReferenceError:MouseEvent未定義

(我嘗試添加NG2選在角CLI生成的項目,它工作得很好,雖然)

只需添加這兩條線後:

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 
    } 
} 

回答

0

我想這個錯誤是由於指令加載不正確而引起的。

1)確保正確的組件正在導入,並驗證是否MouseEvent定義

2)驗證是否已出口所有組件;

3)

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

確保SELECT_DIRECTIVES不是數組對象。如果是這樣,我想你應該嘗試使用

[ ...SELECT_DIRECTIVES ]

+0

感謝,但還是同樣的錯誤。請注意'MouseEvent'位於** node_modules **文件夾內。它在我生成的angular-cli項目中效果很好。我想這與** tsconfig.json **有關 –

-1

我發現了這個問題的臨時解決方案。我把文本替換插件正則表達式替換裝載機到的WebPack配置:

module: { rules: [ { test: /\.(ts|js)$/, loader: 'regexp-replace-loader', options: { match: { pattern: '\\[(Mouse|Keyboard)Event\\]', flags: 'g' }, replaceWith: '[]', } }, { test: /\.ts$/, loader: 'ts-loader' }, ] },