ngRoute 为所有路由设置基本 URL [英] ngRoute set base url for all routes

查看:25
本文介绍了ngRoute 为所有路由设置基本 URL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以向 AngularJS 应用程序中的所有路由添加基本网址?基本上改变它在服务器上的位置(有点,如果这有意义的话......所以它不会通过 / 而是通过 /something/ 访问).

Is it possible to add a base url to all routes in an AngularJS app? Essentially changing its location on the server (kind of, if that makes sense... so it would be accessed not via / but via /something/).

为了添加一些上下文,我试图将现有的 Angular 应用程序置于某种身份验证之后,以便现在可以在地址如 http://mysite/secure 上访问该应用程序代码>登录成功后.

To add some context, I am trying to place an existing Angular app behind some authentication such that the app would now be accessed at address say http://mysite/secure after successful login.

问题是,如果我在 http://mysite/secure 加载应用程序,它工作正常(服务器显然会提供正确的页面),但单击任何链接都会导致页面重新加载并路由到 http://mysite/#newpage 而不是 http://mysite/secure/#newpage.

The problem is if I was to load the app at http://mysite/secure it works fine (the server will obviously serve up the correct page), but clicking any link would result in a page reload and route to http://mysite/#newpage instead of http://mysite/secure/#newpage.

如果不将 /secure/ 添加到所有路由和链接元素,这可能吗?干杯,对不起,如果措辞不好.

Without adding /secure/ to all of the routes and link element is this possible? Cheers, sorry if that is not worded well.

推荐答案

设置 HTML5 标签可能会有所帮助.从文档这里:

Setting the <base> HTML5 tag might help. From the documentation here:

相对链接

请务必检查所有相关链接、图像、脚本等.您必须在主 html 文件的头部指定 url base (<base href="/my-base>>) 或者你必须在任何地方使用绝对 url(以/开头),因为相对 url 将使用文档的初始绝对 url 解析为绝对 url,这通常与应用程序的根不同.

Be sure to check all relative links, images, scripts etc. You must either specify the url base in the head of your main html file (<base href="/my-base">) or you must use absolute urls (starting with /) everywhere because relative urls will be resolved to absolute urls using the initial absolute url of the document, which is often different from the root of the application.

强烈建议在文档根目录启用 History API 的情况下运行 Angular 应用,因为它会处理所有相关链接问题.

Running Angular apps with the History API enabled from document root is strongly encouraged as it takes care of all relative link issues.

这篇关于ngRoute 为所有路由设置基本 URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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