我是 React 新手,目前正在开发导航栏。我有 index.js 这是我的启动文件
import React from 'react';
import ReactDOM from 'react-dom';
import {BrowserRouter} from 'react-router-dom';
import history from 'history';
import Routes from './routes/index';
import Template from './containers/Template';
ReactDOM.render(
(
<BrowserRouter
history={history}
routes={Routes}
>
<Template />
</BrowserRouter>
),document.getElementById('root')
);
路由是从如下所示的 routes/index.js 文件中导入的
import React from 'react';
import {Route, BrowserRouter, Switch} from 'react-router-dom';
import Template from '../containers/Template';
import Home from '../containers/Home';
import Profile from '../containers/Profile';
const createRoutes = () => {
return (
<BrowserRouter>
<Switch>
<Route path= '/' component= {Template}/>
<Route path= '/' component={Home}/>
<Route path= {'/profile'} component={Profile}/>
</Switch>
</BrowserRouter>
)
}
const Routes = createRoutes();
export default Routes;
我的主要问题是,当我使用 chrome 和 React Developer 工具时,我可以看到与 BrowserRouter 对象相关的路由,如下所示 浏览器元素检查中的路由
但是我无法打开任何指定的路由,总是得到“无法获取 /profile”,请注意我使用 webpack 作为 web 开发包。