[英]How to fix 'Unhandled promise rejection TypeError: Object doesn't support property or method 'forEach'' error in IE11
我正在嘗試解決IE 11中的問題,但是我不知道該怎么辦。 我正在使用新的代碼樣式ES6進行編程,因此我必須將代碼從ES6轉換為ES5(我使用babel進行轉換,並使用匯總來捆綁... gulp作為任務管理器)。
IE 11報告此錯誤:
Unhandled promise rejection TypeError: Object doesn't support property or method 'forEach'
為了理解...是一個自動建議的搜索組件。 您可以在輸入上寫一些東西,我的js組件會將請求發送到服務器(Promise),服務器返回帶有數據的json對象。然后我對其進行處理並在網站上提出建議。
我不知道哪里出了問題。 我以為這是forEach方法的問題,但我可能沒有使用polyfills(但也許是我的babel或匯總設置的問題...我真的不知道:/)
這是最可能出現的問題:
...
requestResult: function(_selector, _inputValue) {
let self = this;
let elRequestURL = _selector.dataset.service;
let promiseObj = new Promise(function(resolve, reject) {
let requestURL = _inputValue === '' ? elRequestURL : elRequestURL + '?query=' + _inputValue;
let request = new XMLHttpRequest();
let response = [];
request.open('GET', requestURL);
request.responseType = 'json';
request.send();
request.onreadystatechange = function() {
_selector.classList.add('is-loading');
if (request.readyState === 4){
if (request.status === 200){
var response = request.response;
_selector.classList.remove('is-loading');
resolve(response);
} else {
reject(request.status);
console.log("xhr failed");
}
}
}
});
return promiseObj;
},
...
(如果有必要,我可以發送完整的代碼)
Promise可能是問題嗎? 您是否有此錯誤的經驗或任何提示,我該如何解決? Internet Explorer沒有顯示問題所在。
IE瀏覽器不支持Promise對象,如果我們想在IE瀏覽器中使用它,我們可以使用編譯器(如Babel )將您的ES6代碼轉換為ES5代碼。 這是有關promise對象的線程 ,您可以檢查它。
未處理的承諾拒絕TypeError:對象不支持屬性或方法'forEach'
眾所周知,IE9及更高版本支持Array.forEach,但不支持NodeList.prototype.forEach(),更多詳細信息,請查看本文 。
如果要在IE瀏覽器中使用forEach方法,則可以添加以下polyfill(在使用forEach()方法之前)。
if ('NodeList' in window && !NodeList.prototype.forEach) {
console.info('polyfill for IE11');
NodeList.prototype.forEach = function (callback, thisArg) {
thisArg = thisArg || window;
for (var i = 0; i < this.length; i++) {
callback.call(thisArg, this[i], i, this);
}
};
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.