反应路由器 4“未找到"当我在浏览器中输入 URL 时 [英] React router 4 "NOT FOUND" when I type in the URL in browser

查看:42
本文介绍了反应路由器 4“未找到"当我在浏览器中输入 URL 时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试学习 react router 4,但遇到了一些问题.我按照这里的指南 https://reacttraining.com/react-router/web/example/no-match,是的,这个示例在我的浏览器中运行良好.我可以点击链接并在浏览器中显示内容.

I am trying to learn react router 4 but meet some problems. I follow the guide from here https://reacttraining.com/react-router/web/example/no-match, and yes this example works fine in my browser. I can click the links and get things shown in the browser.

但是,如果我在浏览器中输入网址而不是点击链接,页面会显示未找到

but, if I type in the url in the browser instead of click the links, the page shows NOT FOUND

未找到页面

发生了什么?React-router 是否只能使用链接而不是输入 URL??

What's happening? Does React-router can only use links instead of typing in URL??

推荐答案

Stijn de Witt 在此处解释这个问题":https://stackoverflow.com/a/36623117/8272354

Stijn de Witt explain about this "problem" here: https://stackoverflow.com/a/36623117/8272354

当您使用路由系统初始路由"中的 URL 进行初始化时,所有路由都知道"如何处理路由器链接.因此,React Link 不会刷新浏览器,只需在 javascript 内部更改路由并加载指定的路由.这样一切正常.

When you initialize with the URL in the "inital route" of your route system, all the routes "know" how to deal with Router Links. So, the React Link does't refresh the browser, just change the route internally in javascript and load the specifyc route. This way everything works fine.

但是,当您直接在 URL 中键入路由(而不是初始路由")时,路由器系统无法识别该路由,浏览器会尝试在服务器中获取此页面.

But, when you type the route (not the "initial route") directly in the URL, the router system does't understand that route, and the browser will try get this page in the server.

这篇关于反应路由器 4“未找到"当我在浏览器中输入 URL 时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆