ric*_*ich 18 javascript reactjs react-router react-router-v4
我怎样才能https://cdnjs.cloudflare.com/ajax/libs/react-router/4.0.0-2/react-router.min.js
使用https://cdnjs.cloudflare.com/ajax/libs/react-router/3.0.1/ReactRouter.min.js
?
使用3.x以下的示例.
HTML
<script src="https://unpkg.com/react@15.4.2/dist/react.min.js"></script>
<script src="https://unpkg.com/react-dom@15.4.2/dist/react-dom.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-router/3.0.1/ReactRouter.min.js"></script>
Run Code Online (Sandbox Code Playgroud)
JS
let { Router, IndexRoute, Redirect, Route, Link, browserHistory } = ReactRouter;
history.replaceState(0,0,'/');
const Main = () =>
<Router history={browserHistory}>
<Route path='/' component={App}>
<IndexRoute component={Home}/>
<Route path='map' component={Map}/>
<Route path='settings' component={Settings}/>
<Redirect from='foo' to='/' />
<Route path='*' component={NotFound}/>
</Route>
</Router>
const App = props =>
<div>
<Navigation>
<Link to='/map'>Map</Link>
<Link to='/settings'>Settings</Link>
<Link to='/foo'>Foo</Link>
</Navigation>
{props.children}
</div>
const Navigation = props => <nav {...props} />
const Home = () => <h1>Home</h1>
const Map = () => <h1>Map</h1>
const Settings = () => <h1>Settings</h1>
const NotFound = (props) => <h1>404 - Not Found</h1>
ReactDOM.render(<Main />, document.body);
Run Code Online (Sandbox Code Playgroud)
请参阅以下网址:https://jsfiddle.net/developit/nvpr63eg/
如果我移动到任何CDN托管的4.x版本虽然它不起作用(返回语句后无法访问的代码).
Bru*_*oLM 13
我可以通过改变一些东西来配置它与redux:
首先,browserHistory
不再定义react-router
.恢复它的一种方法是使用包history
.
您需要安装(redux可选):
npm i -S history react-router react-router-redux
Run Code Online (Sandbox Code Playgroud)
而不是尝试导入browserHistory
使用:
import { createBrowserHistory } from 'history';
Run Code Online (Sandbox Code Playgroud)
如果要使用redux,请将其导入并将其添加到组合缩减器中:
import { routerReducer as routing } from 'react-router-redux';
combineReducers({
home, about,
routing, // new
});
Run Code Online (Sandbox Code Playgroud)
接下来,您将创建该history
对象
// redux
import { syncHistoryWithStore } from 'react-router-redux';
const history = syncHistoryWithStore(createBrowserHistory(), store);
// regular
const history = createBrowserHistory();
Run Code Online (Sandbox Code Playgroud)
然后更改路由器以使用新history
对象
<Router history={ history } children={ Routes } />
Run Code Online (Sandbox Code Playgroud)
其他一切都应该是一样的.
如果有人遇到 The prop history is marked as required in Router, but its value is undefined.
因为browserHistory
在react-router中不再可用,请参阅post来使用历史包.
有关
小智 7
这是Paul S在上面发布的补充答案.信用仍然应该转到保罗发布它.
补充答案的理由是bcoz: -
纱线步骤:
yarn add react-router
yarn add react-router-dom
Run Code Online (Sandbox Code Playgroud)
的package.json:
"react": "^15.6.1",
"react-dom": "^15.6.1",
"react-router": "^4.1.1",
"react-router-dom": "^4.1.1"
Run Code Online (Sandbox Code Playgroud)
index.js:
import React from 'react';
import ReactDOM from 'react-dom';
import { Switch, Redirect, Route} from 'react-router';
import {BrowserRouter, Link} from 'react-router-dom';
//let { BrowserRouter, Switch, Redirect, Route, Link } = ReactRouter;
const Main = () =>
<BrowserRouter>
<App>
<Switch>
<Route exact path='/' component={Home}/>
<Route path='/map' component={Map}/>
<Route path='/settings' component={Settings}/>
<Route path='/foo' component={Foo}/>
<Route component={NotFound}/>
</Switch>
</App>
</BrowserRouter>
const App = props =>
<div>
<Navigation>
<Link to='/map'>Map</Link>
<Link to='/settings'>Settings</Link>
<Link to='/foo'>Foo</Link>
</Navigation>
{props.children}
</div>
const Navigation = props => <nav {...props} />
const Home = () => <h1>Home</h1>
const Map = () => <h1>Map</h1>
const Settings = () => <h1>Settings</h1>
const Foo = () => <Redirect to='/' />
const NotFound = (props) => <h1>404 - Not Found</h1>
ReactDOM.render(<Main />, document.body);
Run Code Online (Sandbox Code Playgroud)
应该有一个更接近完整版本的升级指南.我为即将推出的beta而不是当前的alpha修改了代码.测试版还没有发布,所以很遗憾没有React Router的托管版本,你可以用它来测试它.
let { BrowserRouter, Switch, Redirect, Route, Link } = ReactRouter;
const Main = () =>
<BrowserRouter>
<App>
<Switch>
<Route exact path='/' component={Home}/>
<Route path='/map' component={Map}/>
<Route path='/settings' component={Settings}/>
<Route path='/foo' component={Foo}/>
<Route component={NotFound}/>
</Switch>
</App>
</BrowserRouter>
const App = props =>
<div>
<Navigation>
<Link to='/map'>Map</Link>
<Link to='/settings'>Settings</Link>
<Link to='/foo'>Foo</Link>
</Navigation>
{props.children}
</div>
const Navigation = props => <nav {...props} />
const Home = () => <h1>Home</h1>
const Map = () => <h1>Map</h1>
const Settings = () => <h1>Settings</h1>
const Foo = () => <Redirect to='/' />
const NotFound = (props) => <h1>404 - Not Found</h1>
ReactDOM.render(<Main />, document.body);
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
18834 次 |
最近记录: |