gpt4 book ai didi

javascript - react-router-dom 不工作只是渲染 "/"

转载 作者:行者123 更新时间:2023-12-05 07:06:49 25 4
gpt4 key购买 nike

我有一些 throubes 试图渲染多个页面并使用react。我是 React 的新手,这就是为什么我要查看许多页面和教程的原因。我正在使用 React、Webpack、Babel 和 ESLint airbnb

当我渲染我的 React 应用程序时,它看起来像这样。

hows render homepage

我用来渲染所有路由的页面App是这样的

import React from 'react';
import { BrowserRouter, Route, Switch, Redirect } from 'react-router-dom';
import Homepage from './Homepage';
import Layout from '../components/Layout';
import NotFound from './NotFound';
// import NotFound from './NotFound';

function App() {
return (
<BrowserRouter>
<Layout>
<Switch>
<Route exact path='/' component={Homepage} />
<Route exact path='/homepage' component={Homepage} />
<Route path='/404' component={NotFound} />
<Redirect from='*' to='/404' />
</Switch>
</Layout>
</BrowserRouter>
);
}

export default App;

然后当我转到路线/homepage 或任何看起来像这样的路线时

render nothing

NotFound页面代码是这样的

import React from 'react';

function NotFound() {
return <h1> Error 404: Not Found</h1>;
}

export default NotFound;

我的布局是这样的

import React from 'react';
import Header from './Header';
import Footer from './Footer';

function Layout(props) {
const { children } = props;
return (
<>
<Header />
{children}
<Footer />
</>
);
};

export default Layout;

我正在寻找解决问题的答案,也许是我使用的 webpack 配置,但我不确定。我的 webpack 看起来像这样。

const path = require('path');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const MiniCssExtractPlugin = require('mini-css-extract-plugin');

module.exports = {
// para definir nuestra entrada index.js y la salida nuestro archivo bunble.js
entry: './src/index.js',
output: {
path: path.resolve(__dirname, 'dist'),
filename: 'bundle.js',
},
// para resolver nuestros elementos
resolve: {
extensions: ['.js', '.jsx'],
},
module: {
rules: [
{
test: /\.(js|jsx)$/,
exclude: /node_modules/,
use: {
loader: 'babel-loader',
},
},
{
test: /\.html$/,
use: {
loader: 'html-loader',
},
},
{
test: /\.(s*)css$/,
use: [
{ loader: MiniCssExtractPlugin.loader },
'css-loader',
'sass-loader',
],
},
{
test: /\.(png|gif|jpg)$/,
use: [
{
loader: 'file-loader',
options: { name: 'assets/[hash].[ext]' },
},
],
},
],
},
plugins: [
new HtmlWebpackPlugin({
template: './public/index.html',
filename: './index.html',
}),
new MiniCssExtractPlugin({
filename: 'assets/[name].css',
}),
],
};

我的package.json是这样的

{
"name": "legendary-deca-meca-app",
"version": "1.0.0",
"description": "Legendary app ",
"main": "index.js",
"dependencies": {
"react": "^16.13.1",
"react-dom": "^16.13.1",
"react-router-dom": "^5.2.0",
"react-scripts": "^3.4.1"
},
"devDependencies": {
"@babel/core": "^7.10.2",
"@babel/preset-env": "^7.10.2",
"@babel/preset-react": "^7.10.1",
"babel-eslint": "^10.1.0",
"babel-loader": "^8.1.0",
"css-loader": "^3.5.3",
"eslint": "^7.2.0",
"eslint-config-airbnb": "^18.1.0",
"eslint-plugin-import": "^2.21.2",
"eslint-plugin-jsx-a11y": "^6.2.3",
"eslint-plugin-react": "^7.20.0",
"file-loader": "^6.0.0",
"html-loader": "^1.1.0",
"html-webpack-plugin": "^4.3.0",
"mini-css-extract-plugin": "^0.9.0",
"node-sass": "^4.14.1",
"sass-loader": "^8.0.2",
"webpack": "^4.43.0",
"webpack-cli": "^3.3.11",
"webpack-dev-server": "^3.11.0"
},
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"build": "webpack --mode production",
"start": "webpack-dev-server --open --mode development"
},
"repository": {
"type": "git",
"url": "git+https://github.com/RaPzoD1/legendary-deca-meca-app.git"
},
"keywords": [],
"author": "",
"license": "ISC",
"bugs": {
"url": "https://github.com/RaPzoD1/legendary-deca-meca-app/issues"
},
"homepage": "https://github.com/RaPzoD1/legendary-deca-meca-app#readme"
}

感谢您的意见和时间。

最佳答案

还不能评论。 :)

你有没有试过将你的“/”路由移动到 block 的底部,像这样:

        <Switch>
<Route path='/homepage' component={Homepage} />
<Route path='/404' component={NotFound} />
<Route exact path='/' component={Homepage} />
<Redirect from='*' to='/404' />
</Switch>

关于javascript - react-router-dom 不工作只是渲染 "/",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62368171/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com