【问题标题】:SCRIPT438: Object doesn't support property or method 'assign'SCRIPT438:对象不支持属性或方法“分配”
【发布时间】:2019-02-09 03:44:56
【问题描述】:

我将 React 与 Redux 一起使用,但我遇到了 IE 问题。仅在 IE 上不起作用。

当我只使用 react 构建应用程序时,它可以工作,所以我猜测它与 Redux 有关。它为应用返回 undefined 并且 redux-logger 显示此错误:

SCRIPT438:对象不支持属性或方法“assign”

我的 webpack:

const path = require("path");
const componentName = "contact-captain";
const publicFolderRelativePath = "../../../../public/js";
module.exports = {
    output: {
        path: path.resolve(__dirname, publicFolderRelativePath),
        filename: `${componentName}.js`
    },
    module: {
        rules: [
            {
                test: /\.js$/,
                exclude: /node_modules/,
                use: {
                    loader: "babel-loader"
                }
            }
        ]
    }
};

package.json

{
  "name": "temp",
  "version": "1.0.0",
  "main": "index.js",
  "scripts": {
    "watch": "webpack -w --mode development --progress --color --display-error-details",
    "build": "webpack --mode production"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "description": "",
  "devDependencies": {
    "@babel/core": "^7.0.0",
    "@babel/plugin-transform-object-assign": "^7.0.0",
    "@babel/preset-env": "^7.0.0",
    "@babel/preset-react": "^7.0.0",
    "babel-loader": "^8.0.2",
    "react": "^16.4.2",
    "react-dom": "^16.4.2",
    "react-redux": "^5.0.7",
    "redux": "^4.0.0",
    "redux-logger": "^3.0.6",
    "redux-thunk": "^2.3.0",
    "webpack": "^4.17.1",
    "webpack-cli": "^3.1.0"
  },
  "dependencies": {
    "axios": "^0.18.0",
    "lodash": "^4.17.10",
    "moment": "^2.22.2"
  }
}

.babelrc

{
  "presets": ["@babel/preset-env", "@babel/preset-react"],
  "plugins": [ "@babel/plugin-transform-object-assign"]
}

当我使用 babel-polyfill 时它可以工作,但我不能使用它,因为在加载 babel-polyfill 的页面上已经使用了它,所以它为那个不同的组件返回错误。

【问题讨论】:

    标签: reactjs webpack redux babeljs


    【解决方案1】:

    添加这个对我有帮助

      if (typeof Object.assign != 'function') {
            Object.assign = function(target) {
                'use strict';
                if (target == null) {
                    throw new TypeError('Cannot convert undefined or null to object');
                }
    
                target = Object(target);
                for (var index = 1; index < arguments.length; index++) {
                    var source = arguments[index];
                    if (source != null) {
                        for (var key in source) {
                            if (Object.prototype.hasOwnProperty.call(source, key)) {
                                target[key] = source[key];
                            }
                        }
                    }
                }
                return target;
            };
        }
    

    【讨论】:

    • 此解决方案还解决了加载具有 Facebook 聊天插件的页面时 IE11 引发的错误。添加代码后,FB 聊天开始在 IE 上运行。谢谢@olga_babic。
    【解决方案2】:

    考虑将 https://babeljs.io/docs/en/babel-plugin-transform-object-assign 添加到您的 .babelrc 以使其正常工作

    【讨论】:

      猜你喜欢
      • 2021-05-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-22
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多