英文:
Manually access a route with react router and a spring boot backend
问题
我有一个React用户界面,可以在新标签页中打开另一个用户界面的子路由。
例如:在localhost/app1
中的一个按钮可以打开一个新标签页到localhost/app2/usersettings
- 这在开发环境中可以正常工作,但一旦我将应用程序打包部署,它就会失败。问题是它只是重定向我回到localhost/app2
。
这是我的设置:
@Override
public void addViewControllers(ViewControllerRegistry registry) {
registry.addRedirectViewController("/", "index.html");
registry.addRedirectViewController("/usersettings", "index.html?path=usersettings");
registry.addRedirectViewController("/{x:[\\w\\-]+}", "index.html");
registry.addRedirectViewController("/{x:^(?!api$).*$}/**/{y:[\\w\\-]+}", "index.html");
}
const MainPage = () => {
return (
<Box>
<CssBaseline/>
<Routes>
<Route path="/" element={<HomePage/>}/>
<Route path="index.html" element={<Navigate to="/"/>}/>
<Route path="/usersettings" element={<UserSettings/>}/>
</Routes>
</Box>
);
};
如果我在app2中导航到/usersettings
,如果我点击路由,它可以完美工作,但一旦我手动输入URL或者通过app1的按钮重定向,它只会加载主页路由。
我应该如何设置路由,以便路由器接收到正确的路径变量?
我完全迷失了。
谢谢。
英文:
I have a react UI that opens a new tab to another UI's sub route.
Example: a button in localhost/app1
opens a new tab to localhost/app2/usersettings
- This works in a development scenario but once I roll out the applications as package, it fails. The issue is, that it just redirects me back to localhost/apps2
Here's my setup
@Override
public void addViewControllers(ViewControllerRegistry registry) {
registry.addRedirectViewController("/","index.html");
registry.addRedirectViewController("/usersettings","index.html?path=usersettings");
registry.addRedirectViewController("/{x:[\\w\\-]+}","index.html");
registry.addRedirectViewController("/{x:^(?!api$).*$}/**/{y:[\\w\\-]+}", "index.html");
}
const MainPage = () => {
return (
<Box>
<CssBaseline/>
<Routes>
<Route path="/" element={<HomePage/>}/>
<Route path="index.html" element={<Navigate to="/"/>}/>
<Route path="/usersettings" element={<UserSettings/>}/>
</Routes>
</Box>
);
};
Navigating to /usersettings
in app2 works perfectly if I click the route, but once I manually enter the url or redirect by a button from app1 it just loads the home route.
How am I supposed to setup the routes so router receives the correct path variable?
I am totally lost.
Thanks
答案1
得分: 1
首先,您需要配置您的服务器,以便为您的React应用程序应处理的所有路由提供正确的index.html
文件,正如我所说的。为此,您可以通过设置一个捕获所有路由的路由来实现,以为任何未识别的路由提供index.html
文件。
让我用一个示例来解释给您:
@Override
public void addViewControllers(ViewControllerRegistry registry) {
registry.addRedirectViewController("/", "index.html");
registry.addRedirectViewController("/{x:^(?!api$).*$}/**", "index.html");
}
这应该允许您的React应用程序正确处理所有路由,包括在新标签页中打开的路由或从app1
重定向的路由。
在您的React应用程序中,您可以像这样设置您的路由:
const MainPage = () => {
return (
<Box>
<CssBaseline/>
<Routes>
<Route path="/" element={<HomePage/>}/>
<Route path="/usersettings" element={<UserSettings/>}/>
<Route path="*" element={<Navigate to="/"/>}/>
</Routes>
</Box>
);
};
正如您所见,配置设置了一个捕获所有路由并将其重定向到主页的路由,以确保您的React应用程序正确处理所有路由。
祝你好运,朋友!
英文:
Alrighty, first of all you need to configure your server to serve the correct index.html
file for all routes that should be handled by your React application, as I told. for that purpose, you maybe should do this by setting up a catch-all route that serves the index.html
file for any unrecognized routes.
let me explain it for you with an example :
@Override
public void addViewControllers(ViewControllerRegistry registry) {
registry.addRedirectViewController("/", "index.html");
registry.addRedirectViewController("/{x:^(?!api$).*$}/**", "index.html");
}
it should allow your React application to handle all routes correctly, including routes that are opened in a new tab or redirected from app1
.
and in your React application,you can then set up your routes like this:
const MainPage = () => {
return (
<Box>
<CssBaseline/>
<Routes>
<Route path="/" element={<HomePage/>}/>
<Route path="/usersettings" element={<UserSettings/>}/>
<Route path="*" element={<Navigate to="/"/>}/>
</Routes>
</Box>
);
};
as you see,configuration sets up a catch-all route that redirects to the home page for any unrecognized routes and it ensures that all routes are handled correctly by your React application
good luck bro !
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论