0
import React from 'react';
import {
  Route,
  Router,
  Switch
} from 'react-router';
import history from '../history';
import App from '../App'
const router = () => (
  <Router history={history}>
    <Switch>
      <Route exact path="/" component={App} />
    </Switch>
  </Router>
);

export default router;

打开页面是空白的,但测试环境是好的
history文件

/*导入history包,路由跳转时使用*/
import { createBrowserHistory } from 'history'
export default createBrowserHistory()

希望大神能看下是什么问题

笑场 64
2019-04-26 提问
2 个回答
0

codepen 上无法测试哈哈。。。

本地上试了一下,你的代码没有任何问题。
瑕疵:

  1. 既然是 React组件,还是大写开头吧。 const router , export default router;
  2. 你是不是忘记 ReactDOM.render了?
<!doctype html>
<html>

<head>
    <meta charSet="UTF-8" />
    <title></title>
    <meta name="description" />
    <meta name="keywords" />
</head>

<body>
    <div id='app'></div>
    <script src='https://cdnjs.cloudflare.com/ajax/libs/react/16.8.6/umd/react.production.min.js'></script>
    <script src='https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.8.6/umd/react-dom.production.min.js'></script>
    <script src='https://cdnjs.cloudflare.com/ajax/libs/react-router-dom/5.0.0/react-router-dom.min.js'></script>
    <script src='https://cdnjs.cloudflare.com/ajax/libs/babel-core/5.8.34/browser.min.js'></script>
    <script src='https://unpkg.com/history@4.9.0/umd/history.js'></script>
    <script src='https://cdnjs.cloudflare.com/ajax/libs/es6-shim/0.35.5/es6-shim.min.js'></script>
    <script type='text/babel'>
        'use strict';
       
 const {Router,Switch,Route} =   ReactRouterDOM

const history2= window.History.createBrowserHistory()
const App=()=><h2>你好</h2>
const App2 = () => (
  <Router history={history2}>
    <Switch>
      <Route exact path="/" component={App} />
    </Switch>
  </Router>
);

ReactDOM.render(<App2 />, document.getElementById("app"));
</script>
</body>

</html>

本地结果

0

在服务器上部署的,需要按照SPA的形式部署的。

撰写答案

推广链接