【问题标题】:ReactJS - issue about react-router-domReactJS - 关于 react-router-dom 的问题
【发布时间】:2019-07-25 02:55:47
【问题描述】:

我有一个 <Routes> 组件,它只呈现一个 <Dashboard> 组件。当我尝试获取 <BillingCycle> 组件时,没有发生同样的事情。当我在浏览器中输入可能获取 BillingCycle' 页面的 URL 时,Billing Cycles 内容没有出现。继续显示仪表板内容。我错了什么?谢谢你。

导入'../common/template/dependencies' 从“反应”导入反应

这里是导入 Routes 组件的父组件。

import Routes from './Routes'

    export default (props) => (
      <div className='wrapper'>  
        <div className='content-wrapper'>
         <Routes />
        </div>
      </div>
    )

这里是成功出现在这些 url 中的仪表板组件:http://localhost:8080http://localhost:8080/#/

import React from 'react'

export default props  => (
  <div>
    <h1>Dashboard</h1>
  </div>
)

这里是当我输入它的 url 时没有出现的 billingCycle 组件: http://localhost:8080/#/billingCycles

import React from 'react'

export default props => {

  return (
   <h1>Ciclo de pagamentos</h1>
  )
}

这里是路由组件:

import React from 'react'
import { BrowserRouter as Router, Route, Redirect, Switch } from 'react-router-dom';

import Dashboard from '../dashboard/Dashboard'
import BillingCycle from '../billingCycle/BillingCycle'

export default props => (
  <Router>
    <Switch>      

      <Route exact path='#/billingCycles' component={BillingCycle} />
      <Route exact path='/' component={Dashboard} />
      <Redirect from='*' to='/' />
    </Switch>
  </Router>
)

【问题讨论】:

  • 为了做到这一点,您必须使用服务器端渲染或也称为同构。

标签: reactjs react-router-dom


【解决方案1】:

如果你想在 URL 中使用哈希,你应该使用HashRouter。而且你不应该在路由中添加哈希:

<Route exact path='/billingCycles' component={BillingCycle} />

【讨论】:

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-01-26
  • 2019-02-05
  • 2019-06-28
  • 2019-03-17
  • 2019-08-09
  • 1970-01-01
相关资源
最近更新 更多