javascript BrowserRouter v4 - 'BrowserRouter' 未定义 react/jsx-no-undef

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/46776219/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 06:54:25  来源:igfitidea点击:

BrowserRouter v4 - 'BrowserRouter' is not defined react/jsx-no-undef

javascriptnode.jsreactjs

提问by Thomas Hennes

I want to use BrowserRouter in my react app. I have nested my top most component in the BrowserRouter tags as shown:

我想在我的 React 应用程序中使用 BrowserRouter。我在 BrowserRouter 标签中嵌套了最顶层的组件,如下所示:

import React from 'react';
import ReactDOM from 'react-dom';
import { BrowserRouter as Router, Route, Link } from 'react-router-dom';
import App from './App';
import registerServiceWorker from './registerServiceWorker';


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

registerServiceWorker();

And I am getting the following error:

我收到以下错误:

./src/index.js
Line 9:  'BrowserRouter' is not defined  react/jsx-no-undef

I have installed react-router-dom and the dependancy is in my package.json file. So why am I getting this?

我已经安装了 react-router-dom 并且依赖在我的 package.json 文件中。那为什么我会得到这个?

回答by Thomas Hennes

You imported it as Router, so you should call it as <Router>as well.

您将其导入为Router,因此您也应该调用它<Router>