reactjs Babel Preset 不支持 IE11 的 Object.assign - “Object 不支持属性或方法‘assign’”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/49236325/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Babel Preset does not provide support on IE11 for Object.assign - "Object doesn't support property or method 'assign'"
提问by Ayesha Mundu
I am using babel-preset-env version - 1.6.1for my react app, i am getting a error on IE :- Object doesn't support property or method 'assign'
我正在为我的 react 应用程序使用babel-preset-env 版本 - 1.6.1,我在 IE 上遇到错误:-对象不支持属性或方法“分配”
this is my .babelrc:-
这是我的.babelrc:-
{
"presets": [
"react",
[
"env",
{
"targets": {
"browsers": [
"last 1 versions",
"ie >= 11"
]
},
"debug": true,
"modules": "commonjs"
}
]
],
"env": {
"test": {
"presets": [
[
"babel-preset-env",
"react"
]
],
"plugins": [
"transform-object-rest-spread",
"transform-class-properties",
"transform-runtime",
"babel-plugin-dynamic-import-node",
"array-includes",
"url-search-params-polyfill",
"transform-object-assign"
]
}
},
"plugins": [
"transform-object-rest-spread",
"transform-class-properties",
"syntax-dynamic-import",
"transform-runtime",
"array-includes",
"url-search-params-polyfill",
"transform-object-assign"
]
}
}
i tried these polyfills :-
我试过这些 polyfills:-
https://babeljs.io/docs/plugins/transform-object-assign/https://www.npmjs.com/package/babel-plugin-object-assign
https://babeljs.io/docs/plugins/transform-object-assign/ https://www.npmjs.com/package/babel-plugin-object-assign
but it didn't work
但它没有用
i am using syntax:-
我正在使用语法:-
let a = Object.assign({},obj);
everywhere in my project
在我的项目中随处可见
i need a polyfill that would work for my project.
我需要一个适用于我的项目的 polyfill。
回答by Dominik Jankovi?
You need Babel Polyfill.
你需要Babel Polyfill。
Either import it in your entry JS file, or use Webpack.
要么将其导入到您的入口 JS 文件中,要么使用 Webpack。
import "babel-polyfill";
or in webpack.config.js
或在 webpack.config.js
module.exports = {
entry: ["babel-polyfill", "./app/main"]
}
NOTE : babel-polyfillShould be imported on very top else It will not work
注意:babel-polyfill应该在最上面导入它不会工作
回答by Tim Gerhard
Babel Polyfill is outdated as of Babel 7.4.0
Babel Polyfill 自 Babel 7.4.0 起已过时
(Source)
(来源)
Please use core-js instead.
请改用 core-js。
import "core-js/stable";
import "regenerator-runtime/runtime";
Or with Webpack:
或者使用 Webpack:
module.exports = {
entry: ["core-js/stable", "regenerator-runtime/runtime", "./app/main"]
}
In my case the above webpack config did not work!because I was also lacking a promise polyfill. This is the webpack.config I ended up using:
在我的情况下,上面的 webpack 配置不起作用!因为我也缺乏承诺 polyfill。这是我最终使用的 webpack.config:
entry: { 'main': ['core-js/fn/promise', 'core-js/stable/object/assign', './wwwroot/src/app.js'] },

