简体   繁体   English

您可以使用react-router-dom直接在URL中搜索吗?

[英]Can you search directly in the URL with react-router-dom?

The problem is that when I try to copy a route directly I get this error: 问题是当我尝试直接复制路线时,出现此错误:

Cannot Get / home 无法回家

But when I navigate with the menu items I usually render the views, I have tried several ways as browserHistory to achieve that effect but I do not succeed, I wonder, if react-router-dom does not allow navigation directly in the URL or is it an error in my code: 但是,当我使用菜单项进行导航时,通常会渲染视图,因此我尝试了多种方法作为browserHistory来实现该效果,但是我没有成功,我想知道react-router-dom是否不允许直接在URL中导航或这是我的代码中的错误:

 // Dependencies import React, { Component} from 'react' import { render } from 'react-dom' import { BrowserRouter as Router, Route, browserHistory, Link} from 'react-router-dom' //components class App extends Component{ render(){ return( <div> <h1>App!</h1> <ul> <li><Link to="/home"> Home</Link></li> <li><Link to="/about"> About </Link></li> <li><Link to="/contact"> Contact </Link></li> </ul> </div> ) } } class Home extends Component { render() { return( <h2>Home Page!</h2> ) } } class About extends Component{ render() { return( <h2>About Page!</h2> ) } } class Contact extends Component { render() { return( <h2>Contact Page!</h2> ) } } // routes render( <Router history={browserHistory}> <div> <Route component={App} path="/" /> <Route component={Home} path="/home" /> <Route component={About} path="/about" /> <Route component={Contact} path="/contact" /> </div> </Router>, document.getElementById('app') ); 

I have solved it! 我已经解决了! for those who have the same error, I share what I did; 对于那些有相同错误的人,我分享我的所作所为; to see the problem if it was from the server so I had to configure webpack with "historyApiFallback: true" or put "--historyApiCallback" in the script of webpack dev server in the package.json luck :) 看看问题是否来自服务器,所以我必须使用“ historyApiFallback:true”来配置webpack或将“ --historyApiCallback”放在package.json的webpack开发服务器的脚本中:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM