reactjs 带有basename和webpack初始url的React路由器

gzjq41n4  于 12个月前  发布在  React
关注(0)|答案(3)|浏览(195)

你好,我被这个卡住了,想不明白。所以我有一个带有basename - "/ui/"的React router v6,我希望当我打开localhost:8080时,它会自动变成"localhost:8080/ui/",因为真实的视图实际上是在/ui/路由中呈现的。
路由器设置:

<BrowserRouter basename="/ui/">
       ...routes here
    </BrowserRouter>

Webpack开发:

module.exports = {
    mode: 'development',
    devtool: 'cheap-module-source-map',
    devServer: {
        hot: true,
        historyApiFallback: true,
        open: true,
        port: 6969
    }
}

常见的webpack:

module.exports = {
    entry: path.resolve(__dirname, '..', './src/index.tsx'),
    resolve: {
        extensions: ['.tsx', '.ts', '.js'],
    },
    module: {
        rules: [
            {
                test: /\.(ts|js)x?$/,
                exclude: /node_modules/,
                use: [
                    {
                        loader: 'babel-loader',
                    },
                ],
            },
            {
                test: /\.css$/,
                use: ['style-loader', 'css-loader'],
            },
            {
                test: /\.s[ac]ss$/i,
                use: [
                    // Creates `style` nodes from JS strings
                    "style-loader",
                    // Translates CSS into CommonJS
                    "css-loader",
                    // Compiles Sass to CSS
                    "sass-loader"
                ]
            },
            {
                test: /\.(?:ico|gif|png|jpg|jpeg)$/i,
                type: 'asset/resource',
            },
            {
                test: /\.(woff(2)?|eot|ttf|otf|svg|)$/,
                type: 'asset/inline',
            },
        ],
    },
    output: {
        path: path.resolve(__dirname, '..', './build'),
        filename: 'bundle.js',
    },
    plugins: [
        new HtmlWebpackPlugin({
            template: path.resolve(__dirname, '..', './src/public/ui/index.html'),
        }),
    ],
    stats: 'errors-only',

}
我尝试添加到输出publicPath: '/ui/',但它不工作。或者类似这样的东西:

static: [
            {
                publicPath: '/',
                directory: path.join(__dirname, "../public/ui/"),
            }]

如何在不“重定向”的情况下实现此操作:\

pnwntuvh

pnwntuvh1#

尝试this solution

<Route exact path="/">
  <Redirect to="/home" />
</Route>
vx6bjr1n

vx6bjr1n2#

srcsources中检查main.js或类似设备。我的有:

const Root = () => (
  <Provider store={store}>
    <Router history={history}>
      <Route path="/" component={App}>
        {appRoute}
      </Route>
    </Router>
  </Provider>
);

所以只能把它移到Route path="/ui/" component={App}

p4tfgftt

p4tfgftt3#

我知道这是一年前的帖子,但它可能会帮助别人。
我也遇到过类似的问题,因为从react-router-dom版本6开始,basename不再自动重定向到basename路径。
我的解决方案是运行以下代码:

if (!window.location.pathname.includes(process.env.PUBLIC_URL)) {
    const currentPathname = window.location.pathname
    window.location.pathname = process.env.PUBLIC_URL + currentPathname
}

export default function App(){
  return(
    <BrowserRouter basename={process.env.PUBLIC_URL}>
      {// ...more code}
    </BrowserRouter>
  )

相关问题