代码:
import React from "react";
import ReactDOM from "react-dom";
import "./index.css";
import App from "./App";
import * as serviceWorker from "./serviceWorker";
import { BrowserRouter as Router, Route, Switch } from "react-router-dom";
function Hello() {
return <div>hello</div>;
}
ReactDOM.render(
<Router>
<Switch>
<Route exact path="/" component={Hello} />
<Route component={App} />
</Switch>
</Router>,
document.getElementById("root")
);
本地调试,在浏览器中访问首页是可以显示hello的,如下图
但是npm run build后,访问就不行了,只能访问App,<Hello>访问不到,如下图
我猜测:你配置的hello组件的路由是/
默认的App的组件的路由也是/
冲突了,在你的环境下,会匹配你的默认路由/,也就是hello
在npm run build,它就会找它的默认路由/,也就是App