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

ReactJS不呈现到DIV

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

    我尝试运行我的应用程序,但控制台和窗口中没有任何内容,我尝试添加 <script src="target/app.js"></script> 但它犯了错误 Cannot GET /target/app.js . 我也在找工作怎么样 create-react-app ,因为它不使用HTML文件中包含bundle的任何脚本,但没有任何用处。

    这是 webpack.config.js . 但我认为Webpack没有问题。或者它可能是devserver的问题?

    const path = require('path')
    const webpack = require('webpack')
    
    module.exports = {
        entry: {
            app: ['@babel/polyfill', './src/index.js']
        },
        module: {
            rules: [
                {
                    test: /\.scss$/,
                    use: ['style-loader', 'css-loader', 'sass-loader']
                },
                {
                    test: /\.(js|jsx)?$/,
                    include: /node_modules/,
                    use: ['react-hot-loader/webpack'],
                },
                {
                    test: /\.(js|jsx)$/,
                    exclude: /node_modules/,
                    include: path.join(__dirname, 'src'),
                    loader: require.resolve('babel-loader'),
                },
                {
                    test: /\.(js|jsx)$/,
                    loader: 'prettier-loader',
                    exclude: /node_modules/,
                    options: require('./prettier.config')
                },
                {
                    test: /\.(jpe?g|png|gif)$/,
                    use: [{
                        /* inline if smaller than 10 KB, otherwise load as a file */
                        loader: 'url-loader',
                        options: {
                            limit: 10000
                        }
                    }]
                },
                {
                    test: /\.(eot|svg|ttf|woff2?|otf)$/,
                    use: 'file-loader'
                }
            ]
        },
        resolve: {
            extensions: ['*', '.js', '.jsx', '.css', 'scss']
        },
        output: {
            path: path.resolve(__dirname, '/target/'),
            filename: '[name].js',
            chunkFilename: '[name].chunk.js',
            publicPath: '/public/'
        },
        plugins: [
            new webpack.ProvidePlugin({
                R: 'ramda'
            })
        ],
        devServer: {
            contentBase: path.join(__dirname, '/public'),
            port: 3131
        }
    }

    这是 src/index.js . 在这个地方,我看不到任何错误,应该一切正常。

    import React from 'react'
    import {render} from 'react-dom'
    const App = () => (
        <div>
            <h1>Hello world!!</h1>
        </div>
    )
    render(<App />, document.getElementById('root'))

    <!DOCTYPE html>
    <html lang=”en”>
    <head>
        <meta charset=”UTF-8">
        <meta name=”viewport” content=”width=device-width, initial-scale=1.0">
        <title>React</title>
    </head>
    <body>
    <div id=”root”></div>
    </body>
    </html>
    2 回复  |  直到 6 年前
        1
  •  1
  •   Danyal Imran    6 年前

    问题似乎是Webpack配置问题,请检查此修复程序是否有效。

    output: {
        path: path.resolve(__dirname, 'dist/'),
        publicPath: '/dist/',
        filename: 'bundle.js'
    },
    devServer: {
        contentBase: path.join(__dirname, 'public/'),
        port: 3000,
        publicPath: 'http://localhost:3000/dist/'
    },
    
        2
  •  0
  •   Martin    6 年前

    尝试添加斜线- <script src="/target/app.js"></script>