[英]React router wont render components
I wrote a small piece of code to understand react router, I have set up routes /
, /aboutme
and /contact
. 我写了一小段代码来了解React Router,我已经设置了路由
/
, /aboutme
和/contact
。 The problem is only the /
route works, The other routes won't render. 问题是仅
/
路由有效,其他路由无法渲染。 There is no error in the console either and the /
route works at localhost:8080/#!/
or localhost:8080/#!
控制台中也没有错误,并且
/
路由在localhost:8080/#!/
或localhost:8080/#!
not at localhost:8080
as I thought it would. 不是我想的那样在
localhost:8080
。 I am using webpack 4 to bundle files. 我正在使用webpack 4捆绑文件。 Is there something wrong with my config file that's causing this issue?
我的配置文件有问题吗?
I tried accessing routes at 我尝试在访问路线
http://localhost:8080/#!/aboutme
http://localhost:8080/#!/contact
only the root component is rendered in both the cases 在两种情况下,仅渲染根组件
http://localhost:8080/aboutme
throws cannot get/url http://localhost:8080/aboutme
引发无法获取/ URL
http://localhost:8080/contact
throws cannot get/url http://localhost:8080/contact
抛出无法获取/网址
I don't understand what I am doing wrong please take look. 我不明白我在做什么错,请看一看。
import React from "react"; import ReactDOM from "react-dom"; import { BrowserRouter as Router } from 'react-router-dom' import Route from 'react-router-dom/Route' const root= (props) => { return ( <p>hello</p> ) } const about= (props) => { return ( <p>About me -_-</p> ) } const contact= (props) => { return ( <p>contact info</p> ) } const App = () => { return ( <Router> <div> <Route path="/" exact component={root} /> <Route path="/aboutme" exact component={about} /> <Route path="/contact" exact component={contact} /> </div> </Router> ) } ReactDOM.render(<App />, document.getElementById("index"));
my webpack config 我的webpack配置
const HtmlWebPackPlugin = require("html-webpack-plugin"); const path = require('path'); const htmlPlugin = new HtmlWebPackPlugin({ template: "./src/index.html", filename: "./index.html" }); module.exports = { output: { path: path.resolve(__dirname, 'dist'), filename: 'index_bundle.js', }, module: { rules: [ { test: /\\.js$/, exclude: /node_modules/, use: { loader: "babel-loader" } }, { test: /\\.css$/, exclude: /node_modules/, use: ['style-loader', 'css-loader'] } ] }, plugins: [htmlPlugin] };
Use <Switch />
Component to render only one component for one route and pass exact prop to Route, it will only render if the full path is matched. 使用
<Switch />
组件仅渲染一条路线的一个组件,并将精确的prop传递给Route,只有在完整路径匹配时才渲染。
import React from "react";
import ReactDOM from "react-dom";
import { BrowserRouter as Router, Switch } from "react-router-dom";
import Route from "react-router-dom/Route";
const root = props => {
return <p>hello</p>;
};
const about = props => {
return <p>About me -_-</p>;
};
const contact = props => {
return <p>contact info</p>;
};
const pageNotFound = props => {
return <p>page not found.</p>;
};
const App = () => {
return (
<Router>
<Switch>
<Route path="/" exact component={root} exact />
<Route path="/aboutme" exact component={about} exact />
<Route path="/contact" exact component={contact} exact />
<Route component={pageNotFound} />
</Switch>
</Router>
);
};
ReactDOM.render(<App />, document.getElementById("index"));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.