javascript - React中的缩略图组件问题

标签 javascript reactjs webpack components webpack-style-loader

我已将缩略图组件添加到我的项目中。添加后,我在项目中看到以下错误。该错误如下图所示。 enter image description here

这是我的 webpack.config.js 文件代码,可能会帮助您理解这个问题。那里需要指定一个加载程序。我不知道为此指定的加载程序是什么。有人遇到过同样的问题吗?

有什么帮助吗?

**/*webpack.config.js*/**

/* eslint comma-dangle: ["error",


{"functions": "never", "arrays": "only-multiline", "objects":
 "only-multiline"} ] */

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

const devBuild = process.env.NODE_ENV !== 'production';

const config = {
  entry: [
    'es5-shim/es5-shim',
    'es5-shim/es5-sham',
    'babel-polyfill',
    './app/bundles/HelloWorld/startup/registration',
  ],

  output: {
    filename: 'webpack-bundle.js',
    path: pathLib.resolve(__dirname, '../app/assets/webpack'),
  },

  resolve: {
    extensions: ['.js', '.jsx'],
  },
  plugins: [
    new webpack.EnvironmentPlugin({ NODE_ENV: 'development' }),
  ],
  module: {
    rules: [
      {
        test: require.resolve('react'),
        use: {
          loader: 'imports-loader',
          options: {
            shim: 'es5-shim/es5-shim',
            sham: 'es5-shim/es5-sham',
          }
        },
      },
      {
        test: /\.jsx?$/,
        use: 'babel-loader',
        exclude: /node_modules/,
      },
      {
        test: /\.css$/,  
        include: /node_modules/,  
        loaders: ['style-loader', 'css-loader'],
      },
    ],
  },
};

module.exports = config;

if (devBuild) {
  console.log('Webpack dev build for Rails'); // eslint-disable-line no-console
  module.exports.devtool = 'eval-source-map';
} else {
  console.log('Webpack production build for Rails'); // eslint-disable-line no-console
}

这是我调用该组件的代码:

import React, { Component } from 'react';
import Thumbnail from 'react-native-thumbnail-video';

class VideoThumnail extends Component {
   render() {
     return(
       <div>
         <Thumbnail url="https://www.youtube.com/watch?v=lgj3D5-jJ74" />
       </div>
     );
   }
}

export default VideoThumnail;

最佳答案

您有一条仅适用于 jsx 的规则。尝试在 webpack 中添加 js 扩展

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

我还看到了 es6 synax,所以尝试用这个在项目根目录中添加 .babelrc

{
  "presets": ["stage-0"]
}

并安装babel-preset-env(npm install --save babel-preset-env)

关于javascript - React中的缩略图组件问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50742426/

相关文章:

reactjs - Material 用户界面 : drawer with expandable side menu

javascript - 更改 node_modules 中的包时如何触发 React 的热重载?

javascript - react-hook-form onSubmit 未触发

node.js - 如何拥有项目根目录的绝对需求/导入路径?

php - Laravel CKEditor5 - ClassicEditor 未定义

javascript - 使子 div 的宽度以与父 div 相同的速率增长

javascript - 什么允许 id 属性的字符,以便 jQuery 选择器不会抛出异常?

javascript - 在 EJS 中引用对象时遇到问题

javascript - 无法使用 Extjs 4 表单将数据从网格加载到新窗口

javascript - 使用 Webpack 和 Electron 使用绝对路径导入