admin管理员组

文章数量:1335183

I setup webpack + babel config

webpack.config.js

...
  module: {
    rules: [
      {
        test: /\.(js|jsx)$/,
        exclude: /node_modules/,
        loader: 'babel-loader'
      },
...

.babelrc

{
  "plugins": ["lodash", "transform-object-rest-spread"],
  "presets": [
    ["env", {
      "targets": [
        "> 4%",
        "ie 11",
        "safari 8"
      ]
    }],
    "react",
    "react-optimize"
  ],
  "env": {
    "test": {
      "presets": ["es2015", "react"]
    }
  }
}

In google chrome everything is ok, but in IE 11i have an error

Object doesn't support property or method 'assign'

I setup webpack + babel config

webpack.config.js

...
  module: {
    rules: [
      {
        test: /\.(js|jsx)$/,
        exclude: /node_modules/,
        loader: 'babel-loader'
      },
...

.babelrc

{
  "plugins": ["lodash", "transform-object-rest-spread"],
  "presets": [
    ["env", {
      "targets": [
        "> 4%",
        "ie 11",
        "safari 8"
      ]
    }],
    "react",
    "react-optimize"
  ],
  "env": {
    "test": {
      "presets": ["es2015", "react"]
    }
  }
}

In google chrome everything is ok, but in IE 11i have an error

Object doesn't support property or method 'assign'

Share Improve this question asked May 6, 2017 at 18:26 Pavel PerevezencevPavel Perevezencev 2,9784 gold badges31 silver badges53 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 5

You would need to add the object assign transform as well Check https://babeljs.io/docs/plugins/transform-object-assign/

Also notice that it is best practice to include a polyfill instead. The MDN usually gives a polyfill code for ES2015 features. Check https://developer.mozilla/en/docs/Web/JavaScript/Reference/Global_Objects/Object/assign

For the record, it is:

if (typeof Object.assign != 'function') {
  Object.assign = function(target, varArgs) { // .length of function is 2
    'use strict';
    if (target == null) { // TypeError if undefined or null
      throw new TypeError('Cannot convert undefined or null to object');
    }

    var to = Object(target);

    for (var index = 1; index < arguments.length; index++) {
      var nextSource = arguments[index];

      if (nextSource != null) { // Skip over if undefined or null
        for (var nextKey in nextSource) {
          // Avoid bugs when hasOwnProperty is shadowed
          if (Object.prototype.hasOwnProperty.call(nextSource, nextKey)) {
            to[nextKey] = nextSource[nextKey];
          }
        }
      }
    }
    return to;
  };
}

You want to include this code in your app for browsers that do not support Object.assign. The Babel transform plugin referred above also remends this approach when building an app and not a library.

It looks like babel pilation doesn't work. Object.assign is ECMAScript 2015 feature and chrome supports it without pilation.

本文标签: javascriptObject doesn39t support property or method 39assign39Stack Overflow