无论我访问哪条路由,react-router(v4)都会返回主页,但是当我通过解析url并使用switch语句进行呈现时,它工作正常。react-router not changing页面
import { BrowserRouter as Router, Route, Switch } from 'react-router-dom'
const Home =() => <div>home page</div>
const About =() => <div>about page</div>
render((
<Router>
<Switch>
<Route path="/" component={Home} />
<Route path="/about" component={About} />
</Switch>
</Router>
), document.getElementById('root'))
我使用Apache服务器和Laravel/PHP后台,所以网址看起来像localhost/calendar/public
和localhost/calendar/public/about
,这可能是问题。但是,无论何时我使用其他路由(例如localhost/calendar/public/foo
),服务器都会返回该页面不存在。
编辑
这里有一个方法,我得到它的工作,但它更是一个黑客不是一个解决方案,我仍然不知道为什么原来的问题又出现了。
import { BrowserRouter as Router, Route, Switch } from 'react-router-dom'
const url = new URL(window.location.href)
const isDev = (url.host == "localhost")
const browserApp = (
<Router>
<Switch>
<Route exact path={`${isDev ? "/calendar/public/" : "/"}`} component={Home}/>
<Route path={`${isDev ? "/calendar/public/about" : "/about"}`} component={About}/>
</Switch>
</Router>
)
它们是无状态的功能组件。 –
什么是无石棉功能组件?上面的那些? – Felix