javascript - 添加 react 原生矢量图标以 react 原生与 react 原生网络时出错

标签 javascript react-native babeljs react-native-web react-native-vector-icons

我关注这个tutorialreact-native-web 添加到 react-native 项目。但 将 react-native-vector-icons 添加到项目时出现错误。

./node_modules/react-native-vector-icons/lib/create-icon-set.js
SyntaxError: /home/hamidreza/Desktop/myApp/node_modules/react-native-vector-icons/lib/create-icon-set.js: Support for the experimental syntax 'classProperties' isn't currently enabled (43:22):

  41 | 
  42 |   class Icon extends PureComponent {
> 43 |     static propTypes = {
     |                      ^
  44 |       allowFontScaling: PropTypes.bool,
  45 |       name: IconNamePropType,
  46 |       size: PropTypes.number,

我还将我的 babel.config.js 更改为:

module.exports = {
  presets: ['module:metro-react-native-babel-preset'],
  plugins: [
    [
      '@babel/plugin-proposal-class-properties',
      {
        loose: true,
      },
    ],
  ],
};

module.exports = {
  presets: ['module:metro-react-native-babel-preset'],
  plugins: [
    '@babel/plugin-proposal-class-properties'
  ],
};

但还是有问题。

我该怎么办?

最佳答案

通过添加

path.resolve(appDirectory, 'node_modules/react-native-vector-icons'),

babelLoaderConfiguration 问题已解决。

最终web/webpack.config.js:

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

const appDirectory = path.resolve(__dirname, '../');
const babelLoaderConfiguration = {
  test: /\.js$/,
  include: [
    path.resolve(appDirectory, 'index.web.js'),
    path.resolve(appDirectory, 'src'),
    path.resolve(appDirectory, 'node_modules/react-native-uncompiled'),
    path.resolve(appDirectory, 'node_modules/react-native-vector-icons'),
    path.resolve(appDirectory, 'node_modules/react-native-elements'),
  ],
  use: {
    loader: 'babel-loader',
    options: {
      cacheDirectory: true,
      presets: ['react-native'],
      plugins: ['react-native-web'],
    },
  },
};

const imageLoaderConfiguration = {
  test: /\.(gif|jpe?g|png|svg)$/,
  use: {
    loader: 'url-loader',
    options: {
      name: '[name].[ext]',
    },
  },
};

module.exports = {
  entry: [path.resolve(appDirectory, 'index.web.js')],

  output: {
    filename: 'bundle.web.js',
    path: path.resolve(appDirectory, 'dist'),
  },
  module: {
    rules: [babelLoaderConfiguration, imageLoaderConfiguration],
  },

  resolve: {
    alias: {
      'react-native$': 'react-native-web',
    },
    extensions: ['.web.js', '.js'],
  },
};

关于javascript - 添加 react 原生矢量图标以 react 原生与 react 原生网络时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62237236/

相关文章:

javascript - 如何使用 jquery/javascript 获取精确的 div 定位

javascript - 有没有一种方法可以在 js 函数上使用 babelJS 并即时生成产量?

javascript - three.js ES6 如何只导入特定模块

javascript - 如何在 coffeescript 循环中捕获异步函数的变量?

javascript - 捕获图案周围的尾随/前导空白(如果存在)?

javascript - 如何将 socket.io 导出到 nodejs 中的其他模块中?

ios - React-Native 嵌套数组

ios - 数据库无法加载 WatermelonDB react-native

ios - 如何在 React Native 中设置 iOS 状态栏背景颜色?

javascript - 如何在编译es2015库时将CSS注入(inject)javascript文件