Javascript 为什么BrowserRouter既是路由器又是路由
有人能在下面的代码中解释双重导入发生了什么吗?它嗅到了多态性,但我不认为这是正在发生的事情 这是从。我只是想知道幕后发生了什么Javascript 为什么BrowserRouter既是路由器又是路由,javascript,reactjs,module,react-router,Javascript,Reactjs,Module,React Router,有人能在下面的代码中解释双重导入发生了什么吗?它嗅到了多态性,但我不认为这是正在发生的事情 这是从。我只是想知道幕后发生了什么 import { BrowserRouter as Router, Route } from 'react-router-dom' <Router> <div> <Route exact path="/" component={Home}/> <Route path="/news" component={N
import { BrowserRouter as Router, Route } from 'react-router-dom'
<Router>
<div>
<Route exact path="/" component={Home}/>
<Route path="/news" component={NewsFeed}/>
</div>
</Router>
If the location of the app is / then the UI hierarchy will be something like:<div>
<Home/>
<!-- react-empty: 2 -->
</div>
And if the location of the app is /news then the UI hierarchy will be:<div>
<!-- react-empty: 1 -->
<NewsFeed/>
</div>
从'react Router dom'导入{BrowserRouter as Router,Route}
如果应用程序的位置为/则UI层次结构类似于:
如果应用程序的位置是/news,则UI层次结构将是:
您没有将其“作为路由器和路由”导入
当您编写时,从'module'导入{Something as Alias,SomethingElse}代码>你只是-在本例中是模块。有些东西
变成了别名
没有别名的等效代码将是
import { BrowserRouter, Route } from 'react-router-dom';
<BrowserRouter>
...
</BrowserRouter>
从'react router dom'导入{BrowserRouter,Route};
...
它不是。您正在从react Router dom
模块导入别名为Router
和Route
的BrowserRouter
。解释得很好。非常感谢。