Angular 4 - 新的MouseEvent导致IE11错误

时间:2017-04-27 10:35:59

标签: javascript angular mouseevent internet-explorer-11 angular-cli

IE11上的角度4中的mouseEvent有问题。我正在使用角4,角度cli。

当我激活这行代码时:

let newEvent = new MouseEvent('mouseleave', {bubbles: true});

IE在控制台中抛出此类异常:

TypeError: Object does not support this operation.
   at MenuItemComponent.prototype.onClick (./main.bundle.js:821:13)
   at Anonymous function (Function code:37:9)
   at handleEvent (./vendor.bundle.js:12039:87)
   at callWithDebugContext (./vendor.bundle.js:13247:9)
   at debugHandleEvent (./vendor.bundle.js:12835:5)
   at dispatchEvent (./vendor.bundle.js:9014:5)
   at Anonymous function (./vendor.bundle.js:9604:31)
   at Anonymous function (./vendor.bundle.js:19253:9)
   at ZoneDelegate.prototype.invokeTask (./polyfills.bundle.js:10732:13)
   at onInvokeTask (./vendor.bundle.js:4357:21)
   at ZoneDelegate.prototype.invokeTask (./polyfills.bundle.js:10732:13)
   at Zone.prototype.runTask (./polyfills.bundle.js:10501:21)
   at invoke (./polyfills.bundle.js:10796:21)

我在我的polyfils.ts和重建项目中取消注释了所有内容,我尝试过IE和ES6垫片......

我在谷歌找不到类似的东西,我认为这是一个愚蠢的问题 - 云有人请指点我解决方案吗?

2 个答案:

答案 0 :(得分:2)

尝试html文件:

<div #sample>You need to click me</div>

在component.js文件中:

@ViewChild('sample') sampleEle: ElementRef;

,并将点击事件(如js中那样用作:

this.sampleEle.nativeElement.click();

答案 1 :(得分:1)

您可以通过以下方式欺骗IE:

从&#39; @ angular / core&#39;中导入{Component,OnInit,ElementRef,ViewChild};

...

@ViewChild(&#39; fileInput&#39;)public fileInput:ElementRef;

...

// Trigger button click event
let event;
if (document.createEvent){
  // Only for IE and Firefox
  event = document.createEvent('MouseEvent');
  event.initMouseEvent('click', true, true, window, 0, 0, 0, 0, 0, false, false, false, false, 0, null);
} else {
  // For Chrome
  event = new MouseEvent('click', {bubbles: false});
}
this.button.nativeElement.dispatchEvent(event);