路由器未在index.js中定义

时间:2018-05-18 17:07:11

标签: javascript reactjs

所以基本上这是我的index.js文件和

import React from 'react';
import ReactDOM from 'react-dom';
import App from './App';
import registerServiceWorker from './registerServiceWorker';

ReactDOM.render(
    <Router>
    <App />
    </Router>,
    document.getElementById('root'));
registerServiceWorker();

这是app.js文件

    import React, { Component } from 'react';
import Default from "./components/Default.js"
import {Route,Switch} from 'react-router-dom';

class App extends Component {
  render() {
    return (
       <Switch>          
          <Route path='/' component={Default}/>
        </Switch>
    );
  }
}

export default App;

我在浏览器上运行时没有定义路由器。但是我已经使用npm install react-router

安装了路由器

1 个答案:

答案 0 :(得分:1)

You are missing import for Router

import React from 'react';
import ReactDOM from 'react-dom';
import App from './App';
import registerServiceWorker from './registerServiceWorker';

// missing import
import { BrowserRouter as Router } from 'react-router-dom'

ReactDOM.render(
    <Router>
    <App />
    </Router>,
    document.getElementById('root'));
registerServiceWorker();