代码之家  ›  专栏  ›  技术社区  ›  morne

旧React和IE11 Polyfill

  •  0
  • morne  · 技术社区  · 6 年前

    我运行反应0.13和不能升级(长话短说)。不能使用巴别塔聚填料。 我需要使应用程序ie11兼容,所以我需要实现很多polyfill。

    如何将polyfill包含在单个文件中并使该文件全局可见?

    我有一个只包含polyfill的文件(像这个)

    但我不确定如何包括它只是在默认情况下全球可见。

    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;
        };
    }
    
    1 回复  |  直到 6 年前
        1
  •  1
  •   Karim    6 年前

    您可以实现一个函数,该函数包装所有polyfill并在需要时导入它。

    export function loadCustomPolyfills() {
    
        if (typeof Object.assign != 'function') {
            Object.assign = function(target) {
                ..
            };
        }
    
        //other polyfills
    
    }
    

    在你的入口点文件中,在开头

    import { loadCustomPolyfills } from './custom-polyfills';
    loadCustomPolyfills();