【问题标题】:Webpack cannot get react components but home pageWebpack 无法获取反应组件但主页
【发布时间】:2020-03-21 19:09:26
【问题描述】:

我是 webpack 的新手,所以我正在尝试将它与我的 react express 应用程序一起使用。例如,我创建了一个捆绑包并将其上传到主机,但它只打开主页,当我尝试使用链接访问其他页面时,我得到“无法获取 /about”。和 webpack-dev-server 的行为相同。有我必须使用的插件吗?

webpack.config.js

const path = require("path");
const HtmlWebPackPlugin = require("html-webpack-plugin");
const htmlPlugin = new HtmlWebPackPlugin({
    template: "./client/public/index.html",
    filename: "./index.html",
    favicon: "./client/public/favicon.ico"
})

module.exports = {
    entry: "./client/src/index.js",
    output: {
        path: path.resolve(__dirname, "dist"),
        filename: "[name].js",
        publicPath: "/"
    },
    devtool: "inline-source-map",
    plugins: [
        htmlPlugin
    ],
    module: {
        rules: [
            {
                test: /\.(png|svg|jpg|gif|webp|ico)$/,
                loader: "file-loader"
            },
            {
                test: /\.s?css$/,
                use: ['style-loader', "css-loader", "sass-loader"]
            },
            {
                test: /\.(js|jsx)$/,
                exclude: /node_modules/,
                    loader: "babel-loader",
                    query: {
                        presets: ['@babel/env', '@babel/react'],
                        plugins: ["@babel/plugin-proposal-class-properties"]
                    }
            }
        ]
    }
}

【问题讨论】:

    标签: reactjs express webpack


    【解决方案1】:

    如果您使用 react-router 或路由与任何其他包进行反应,您需要确保您的服务器将所有路由重定向到您的主页,以便 react 可以管理路由。

    【讨论】:

      猜你喜欢
      • 2019-04-24
      • 2018-09-24
      • 1970-01-01
      • 2021-05-03
      • 1970-01-01
      • 1970-01-01
      • 2023-02-11
      • 2016-05-29
      • 2021-04-10
      相关资源
      最近更新 更多