你好,我被这个卡住了,想不明白。所以我有一个带有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/"),
}]
如何在不“重定向”的情况下实现此操作:\
3条答案
按热度按时间pnwntuvh1#
尝试this solution
vx6bjr1n2#
在
src
或sources
中检查main.js
或类似设备。我的有:所以只能把它移到
Route path="/ui/" component={App}
p4tfgftt3#
我知道这是一年前的帖子,但它可能会帮助别人。
我也遇到过类似的问题,因为从
react-router-dom
版本6开始,basename
不再自动重定向到basename路径。我的解决方案是运行以下代码: