[英]When accessing front-end SPA-links from browser, back-end fires finding no route
I have a working front-end single-page-application written in JS (ReactJS), and a working back-end in Phoenix (Elixir).我有一个用 JS (ReactJS) 编写的工作前端单页应用程序,以及一个用 Phoenix (Elixir) 编写的工作后端。 Everything works out fine as long as navigation happens within the application.
只要导航发生在应用程序中,一切都会很好。 However, when I try to access a page in the SPA from the browser, I get a route error fired from Phoenix.
但是,当我尝试从浏览器访问 SPA 中的页面时,我收到来自 Phoenix 的路由错误。 For example:
例如:
no route found for GET /search (PhoenixApp.Router)
is what I get when I access http://localhost:4000/search from the browser.这是我从浏览器访问http://localhost:4000/search 时得到的。
When I access http://localhost:4000/search from the navigation inside the SPA, I get a working page from ReactJS.当我从 SPA 内的导航访问http://localhost:4000/search 时,我从 ReactJS 获得了一个工作页面。
So my question: How can I get ReactJS to get the page, rather than Phoenix?所以我的问题是:如何让 ReactJS 获取页面,而不是 Phoenix?
The standard way to do this for Single-Page Applications is to serve the exact same thing on every GET route as you're serving on /
.为单页应用程序执行此操作的标准方法是在每个 GET 路由上提供与在
/
上提供服务完全相同的内容。 You can add a wildcard GET route to your Phoenix Router and point it to the same thing as the route for /
.您可以向 Phoenix 路由器添加通配符 GET 路由,并将其指向与
/
路由相同的内容。 If /
is served from PageController
's index
function, you should add:如果
/
来自PageController
的index
函数,您应该添加:
get "/*anything", PageController, :index
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.