我正在尝试在一个新的项目中使用这三种方法。我想使用react-router处理路由。
在“开箱即用”的设置下,当我从例如localhost:3000/开始,然后点击例如Link routing to signup页面时,它工作得很好。但是当我尝试进入localhost:3000/signup时,它显示没有路由(因为没有使用Rails创建的路由)。
我怎样才能集成它呢?
发布于 2017-07-25 16:16:53
您可以通过将react-router-dom与HashRouter一起使用来实现这一点,而不是使用BrowserRouter。
发布于 2020-06-04 06:35:10
处理这种情况的正确解决方案是在Rails config/routes.rb文件中定义所有的React路由。
问题是Rails必须知道React正在处理的路由,否则它会试图接管并处理您的应用程序的路由。
因此,假设您的React的前端控制器路由是这样的:root 'pages#index',在config/routes.rb中如下所示:
Rails.application.routes.draw do
root 'pages#index'
end因此,要让您的React路由正常工作,而不让Rails干扰,您可以这样做:
Rails.application.routes.draw do
root 'pages#index'
get 'admin', to: 'pages#index'
get 'admin/dashboard', to: 'pages#index'
get 'admin/user', to: 'pages#index'
end您只需将所有React路由重定向到前端控制器“pages#index”即可。这将会完成这项工作。
https://stackoverflow.com/questions/45036145
复制相似问题