角路由如何优先于静态站点上的文件路径 [英] How does Angular routing take precedence over file paths on a static site

查看:113
本文介绍了角路由如何优先于静态站点上的文件路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我使用文件结构构建静态站点:

If I build a static site with the file structure:

-index.html
-blog/index.html

然后我将一个带有路由的Angular应用放在blog/index.html内,然后转到路由example.com/blog/page/2,它转到了Angular应用中正确的博客页面.从某种意义上讲,它将打开/blog/index.html,并在Angular应用程序中处理/page/2.

And I put an Angular app with routing inside blog/index.html, then go to the route example.com/blog/page/2, it goes to the correct blog page within the Angular application. In a sense, it opens /blog/index.html, and processes /page/2 within the Angular application.

如何?

为什么Apache(或Nginx)不优先于此,并尝试打开/blog/page/2/index.html,但没有找到它,则显示404?

Why doesn't Apache (or Nginx) take precedence over that, and try to open /blog/page/2/index.html, and not finding it, show a 404?

我认为它可以这样工作真是太棒了,但是我不知道它在技术上是怎么可能的.

I think it's pretty awesome that it can work like this, but I can't figure out how it's technically possible.

推荐答案

apache或nginx或您的Web服务器所需要的任何东西都需要正确且有意地配置为提供有角度的应用程序.对于各种Web服务器配置,有无数的指南,但是通常,您将配置任何404错误以重定向到项目根目录index.html页面.但是,在使用REAL 404的情况下,应设置您的角度应用程序以正确处理它.

apache or nginx or whatever your web server is needs to be properly and intentionally configured to serve an angular app. There are countless guides for various web server configs, but generally you will configure any 404 error to redirect to the project root index.html page. In the event of a REAL 404 however, your angular app should be set up to handle it properly.

这仅用于初始页面加载或刷新.一旦angular应用程序启动并运行,这将是一个有争议的问题,因为angular会拦截并处理所有导航,因此它不会再次击中您的Web服务器.角度路由器仅调用所需的文件,并使用浏览器历史记录API将项目推送到您的网络历史记录中,以模拟正常导航.之所以将它们称为单页应用程序",是因为实际上您的Web服务器只提供了一个页面,其余的都是技巧/模拟.

This is for initial page load or refresh ONLY. Once the angular app is up and running, it's a moot point because angular intercepts and handles all navigation so it doesn't hit your web server again. The angular router just calls up the files it needs and uses the browser history API to push items into your web history to simulate normal navigation. They're called Single Page Applications because there is actually only a single page ever served from your web server, the rest is all tricks / simulation.

这篇关于角路由如何优先于静态站点上的文件路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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