使用 HTML5 路由时 Angular 2 的路由器是否损坏? [英] Is Angular 2's Router broken when using HTML5 routes?

查看:19
本文介绍了使用 HTML5 路由时 Angular 2 的路由器是否损坏?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个全新的路由语法听起来不错,现在我认为它确实如此.我放弃了让它工作的尝试,在这里我注意到即使是 Angular 2 页面中的主要示例也不起作用.如果您打开实时示例并尝试它,只需单击它就可以很酷,并且您可以看到 url 中的路径已更改,但它只是更改字符串,如果您复制您的 url 并发送它,则路径并不真正存在对于某人,他们将收到以下错误:

This whole new routes syntaxis sounded to good to be true and now I'm thinking it actually is. I gave up trying to make it work and here I am noticing that not even the main example in the Angular 2 page works. If you open the live example and you try it just clicking it works cool and you can see the path in the url gets changed, but it is only changing the String the path doesn't really exists, if you copy your url and sending it to someone they will get the following error:

{
    "statusCode": 404,
    "error": "Not Found"
}

地狱,即使你只是刷新页面,你也会得到同样的错误.

Hell even if you just refresh the page you will get the same error.

如果您想重现该问题,请打开此 URL:

If you want to reproduce the issue open this URL:

https://angular.io/resources/live-examples/tutorial/ts/plnkr.html

单击实时预览右上角的在单独的窗口中启动预览"按钮,等待它加载页面,当您看到 URL 更改为如下内容时:

Click the "Launch the preview in a separate window" button in the top right of the live preview, wait for it to load the page and when you see the URL changing to something like:

http://run.plnkr.co/KMzM8hkaCyhlnf3b/dashboard

按 F5 刷新页面,你会得到错误.

do an F5 to refresh the page and you'll get the error.

这是一个错误,它应该工作的方式,还是他们甚至没有尝试过的东西?他们是否已经再次开始使用糟糕的路由器实现,或者我只是完全迷失了我试图让它工作的方式?请指教!

Is this a bug, its the way its supposed to work, or something they didn't even try? Are they already starting once again with a bad router implementation or I'm just totally lost on how I'm trying to make it work? Please advice !

我猜我最好使用主题标签实现.

I'm guessing I'm better off using the hashtag implementation.

推荐答案

实际上,由于浏览器中的实际地址正在更新(并且没有 #/hashbang 方法),因此在上传应用程序时出现 404 错误是正常的.默认情况下,HTML5 历史记录用于在 Angular2 中重用.

In fact, it's normal that you have a 404 error when uploading your application since the actual address within the browser is updating (and without # / hashbang approach). By default, HTML5 history is used for reusing in Angular2.

如果你不想出现 404 错误,你需要更新你的服务器来为每个路由路径提供 index.html 文件.

If you want not having a 404 error, you need to update your server to serve the index.html file for each route path.

此链接也可以帮助您:当我刷新我的网站时,我得到一个 404.这是使用 Angular2 和 firebase.

This link could help you as well: When I refresh my website I get a 404. This is with Angular2 and firebase.

希望对你有帮助蒂埃里

这篇关于使用 HTML5 路由时 Angular 2 的路由器是否损坏?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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