Bigcommerce模具:Javascript错误“对象不支持属性或方法'assign'".在Windows Safari和IE中 [英] Bigcommerce Stencil: Javascript Error "Object doesn't support property or method 'assign'" in Windows Safari and IE
问题描述
不确定发生了什么,但是此错误阻止了页面显示.
Not sure what's happening but this error is preventing the page to display.
错误消息指向我的bundle.js文件(带有webpack,babel等的模板包),并且具体指向stencil-utils包中的Object.assign()
方法.这是@bigcommerce/stencil-utils/src/lib/request.js
The error message is pointing to my bundle.js file (stencil bundle with webpack, babel, etc) and specifically to the Object.assign()
method in the stencil-utils package. It's line 27 of @bigcommerce/stencil-utils/src/lib/request.js
这是产生错误的代码部分.
Here's the section of code producing the error.
const defaultOptions = {
method: 'GET',
remote: false,
requestOptions: {
formData: null,
params: {},
config: {},
template: [],
},
};
const options = Object.assign({}, defaultOptions, opts);
const data = options.requestOptions.formData ? options.requestOptions.formData : options.requestOptions.params;
const headers = {
'stencil-config': options.requestOptions.config ? JSON.stringify(options.requestOptions.config) : '{}',
'stencil-options': '{}',
};
关于可能是什么原因的任何想法?
Any ideas of what might be causing this?
推荐答案
It happens you're using the Object.assign
method, which isn't supported by all browsers. Both Internet Explorer and Safari (for Windows) aren't anymore being officially updated.
Anyway, there's a polyfill of Object.assign
in this page. You can apply it in the top of your code.
这是我自己的polyfill,可以避免创建对象/数组引用(使用Image
等附加接口的对象除外).
This is my own polyfill, which does optionally avoid creating objects/arrays references (except for objects using any additional interface like Image
, etc...).
typeof Object.assign !== "function" &&
(function() {
/**
* Return main instance of value.
* @param {...} value
* @returns
*/
function getMainInstance(value) {
// get instance in this format: [object Instance]
var ic = Object.prototype.toString.call(value);
// returns string between '[object ' and ']'
return ic.substring(ic.indexOf(" ") + 1, ic.lastIndexOf("]")).toLowerCase();
}
Object.assign = function(target) {
/* check if target isn't a object */
if (typeof target !== "object") target = {};
/* last target path */
var lastPath = target;
/* list containing target paths */
var locations = [];
/* consume specific array/object */
function consume(source) {
/* iterate each property to copy */
for (var i in source) {
var instance = getMainInstance(source[i]);
if (instance === "object" || instance === "array") {
lastPath =
lastPath[i] =
locations[locations.length] = (instance === "array" ? [] : {})
consume(source[i]);
} else {
lastPath[i] = source[i];
}
}
var len = -- locations.length;
lastPath = locations[--len] || target;
}
for (var i = 1, source; source = arguments[i]; i++) {
if (typeof source === "object") consume(source);
}
return target;
};
})();
这篇关于Bigcommerce模具:Javascript错误“对象不支持属性或方法'assign'".在Windows Safari和IE中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!