在我的React项目中,我正在使用React Router DOM v4进行客户端路由。
指向我的问题陈述的视频链接:https://drive.google.com/file/d/1nHY-dKxZvDylF5GQUTK02I8Mam1d5eru/view?usp=sharing
我已经创建了这样的路线<Route path="/pg-response/:status" component={PaytmResponse} exact={true}/>
这是我的AppRouter.js文件
import React from 'react';
import {BrowserRouter, Route, Switch} from 'react-router-dom';
import {TransitionGroup, CSSTransition} from 'react-transition-group';
import PrivateRoute from './PrivateRoute';
import HomePage from './../components/HomePage';
import AboutUs from './../components/AboutUs';
import ContactUs from './../components/ContactUs';
import PageNotFound from './../components/PageNotFound';
import RestaurantList from '../components/RestaurantList';
import Foodcourt from '../components/Foodcourt';
import RestaurantMenu from '../components/RestaurantMenu';
import UserDetails from '../components/UserDetails';
import OrderConfirmation from '../components/OrderConfirmation';
import CustomerAccount from '../components/CustomerAccount';
import Logout from '../components/sections/Logout';
import RedirectPG from '../components/sections/RedirectPG';
import SodexoResponse from '../components/sections/SodexoResponse';
import OrderFail from '../components/OrderFail';
import PaytmResponse from '../components/sections/PaytmResponse';
export default () => {
return (
<BrowserRouter>
<Route render={({location}) => (
<TransitionGroup>
<CSSTransition key={location.key} timeout={300} classNames="fade">
<Switch location={location}>
<Route path="/" component={HomePage} exact={true}/>
<Route path="/about" component={AboutUs} />
<Route path="/contact" component={ContactUs} />
<Route path="/restaurants" component={RestaurantList} />
<Route path="/foodcourt" component={Foodcourt} />
<Route path="/select-menu" component={RestaurantMenu} />
<PrivateRoute path="/user-details" component={UserDetails} />
<PrivateRoute path="/order-confirmation" component={OrderConfirmation} />
<PrivateRoute path="/payment-failed" component={OrderFail} />
<PrivateRoute path="/my-account" component={CustomerAccount} />
<PrivateRoute path="/logout" component={Logout} />
<PrivateRoute path="/redirect-to-pg" component={RedirectPG} />
<PrivateRoute path="/sodexo-response" component={SodexoResponse} />
<PrivateRoute path="/paytm-response"/>
<Route path="/pg-response/:status" component={PaytmResponse} exact={true}/>
<Route component={PageNotFound} />
</Switch>
</CSSTransition>
</TransitionGroup>
)} />
</BrowserRouter>
);
}
问题是,当我从<Link>
开始沿这条路线行驶时,点击该路线不会出现任何问题。
但是,当我通过从webpack.config.js
文件进行重定向而走这条路线时,会遇到很多错误。
这是我的webpack.config.js文件
const path = require('path');
const express = require('express');
const bodyParser = require('body-parser');
const url = require('url');
const ExtractTextPlugin = require('extract-text-webpack-plugin');
const webpack = require('webpack');
const app = express();
module.exports = (env) => {
const isProduction = env === 'production';
const CSSExtract = new ExtractTextPlugin('styles.css');
return {
entry: ['babel-polyfill','./src/app.js'],
output: {
path : path.join(__dirname, 'public', 'dist'),
filename: 'bundle.js'
},
module: {
rules: [
{
loader: 'babel-loader',
test: /\.js$/,
exclude: /node_modules/
},
{
test: /\.css$/,
use: CSSExtract.extract({
fallback: 'style-loader',
use: [
{
loader: 'css-loader',
options: {
sourceMap: true
}
}
]
})
},
{
test: /\.(png|jp(e*)g|gif|svg)$/,
use: [
{
loader: 'url-loader',
options: {
limit: 8000,
name: 'images/[hash]-[name].[ext]',
publicPath: '/dist/'
}
}
]
},
{
test: /\.(woff|woff2|eot|ttf|otf|mp4)$/,
use: [
{
loader: "file-loader",
options: {
name: 'files/[hash]-[name].[ext]',
publicPath: '/dist/'
}
}
]
}
]
},
plugins: [
CSSExtract,
new webpack.ProvidePlugin({
$: 'jquery',
jQuery: 'jquery',
"window.jQuery": "jquery"
})
],
devtool: isProduction ? 'source-map' : 'cheap-module-eval-source-map',
devServer: {
contentBase: path.join(__dirname, 'public'),
historyApiFallback: true,
publicPath: '/dist/',
setup: (app) => {
app.use(bodyParser.urlencoded({ extended: true }));
app.post('/paytm-response',(req, res) => {
res.redirect(`/pg-response/${req.body.STATUS}`);
})
}
}
}
}
我也在使用historyApiFallback: true
。
问题仍然存在。
答案 0 :(得分:0)
经过大量搜索,我发现在index.html
文件内部,我正在使用脚本和CSS文件的相对路径。
我将它们更改为绝对路径,并且一切正常。
这不是Web Pack或其他任何问题,只是脚本和CSS文件在index.html
文件中导入方式的问题。