在旧版浏览器上使用Fetch进行ReactJS [英] ReactJS with Fetch on older browsers

查看:141
本文介绍了在旧版浏览器上使用Fetch进行ReactJS的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在用Webpack和Babel实现React JS.但是,我无法让Fetch与IE 11一起使用.

.babelrc文件中包含以下内容:

{
  "presets" : ["env", "stage-0", "react"]
}

以及我的webpack.config.js文件中的以下内容:

var webpack = require('webpack');
var path = require('path');

var DIST_DIR = path.resolve(__dirname, 'dist');
var SRC_DIR = path.resolve(__dirname, 'src');

var config = {
  entry: {
    bundle: [
      'babel-polyfill',
      SRC_DIR + '/app/index.js',
    ]
  },
  output: {
    path: DIST_DIR + '/public/js',
    filename: 'bundle.js'
  },
  module: {
    loaders: [
      {
        test: /\.jsx?$/,
        include: SRC_DIR,
        loader: 'babel-loader'
      }
    ]
  }
};

module.exports = config;

根据我的理解,实施babel-preset-env,stage-0和babel-polyfill应该可以将Fetch API转换为较旧的浏览器可以理解的东西.还有其他我想念的东西吗?

解决方案

fetch不是EcmaScript的一部分,而是网络平台的一部分. Babel只是编写最新Javascript的编译器.如果要在较旧的浏览器中使用fetch,则需要类似此版本

的polyfill

获取API:了解详情

I am implementing React JS, with Webpack and Babel. However, I am having trouble getting Fetch to work with IE 11.

I have the following in my .babelrc file:

{
  "presets" : ["env", "stage-0", "react"]
}

and the following in my webpack.config.js file:

var webpack = require('webpack');
var path = require('path');

var DIST_DIR = path.resolve(__dirname, 'dist');
var SRC_DIR = path.resolve(__dirname, 'src');

var config = {
  entry: {
    bundle: [
      'babel-polyfill',
      SRC_DIR + '/app/index.js',
    ]
  },
  output: {
    path: DIST_DIR + '/public/js',
    filename: 'bundle.js'
  },
  module: {
    loaders: [
      {
        test: /\.jsx?$/,
        include: SRC_DIR,
        loader: 'babel-loader'
      }
    ]
  }
};

module.exports = config;

From my understanding implementing babel-preset-env, stage-0, and babel-polyfill should have allowed for the Fetch API to be transpiled into something older browsers could understand. Is there something else that I am missing?

解决方案

fetch isn't part of EcmaScript but its part of the web platform. Babel is only a compiler to write the latest Javascript. If you want to use fetch in older browsers you need a polyfill like this one

Fetch API: read more

这篇关于在旧版浏览器上使用Fetch进行ReactJS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆