Angular 2 中的外部链接不起作用
External linking isn't working in Angular 2
网站示例:http://oktafox.com
它设置了路由,因此当您单击页面或 links 时,url 会更改为类似以下内容:
/articles
/work
/articles/1
但是如果您要单击此 link:http://oktafox.com/articles/1
它抛出 404。我如何设置它才能转到所需的页面而不是 404?
您的服务器端路由似乎需要一些配置。当您点击 http://oktafox.com 时,您的服务器正在发送回索引页面,然后 Angular 启动并设置路由。这就是为什么点击链接在那之后起作用的原因。
但是如果您直接转到 http://oktafox.com/articles,您的服务器正在尝试查找该文档,但它不存在。必须告知您的服务器始终发回索引页,以便 Angular 可以从 URL 读取“/articles”并继续正确路由。
请注意,如果您在 Angular 中切换到基于哈希的路由(例如 http://oktafox.com/#/articles) you may be able to avoid the issue altogether, since the server doesn't see that portion of the route. But it is not recommended: https://angular.io/docs/ts/latest/guide/router.html
Frank Modica 的上述回答一针见血。经过一番挖掘后,我发现这是我的 Nginx 服务器的配置问题。
网站示例:http://oktafox.com
它设置了路由,因此当您单击页面或 links 时,url 会更改为类似以下内容:
/articles
/work
/articles/1
但是如果您要单击此 link:http://oktafox.com/articles/1
它抛出 404。我如何设置它才能转到所需的页面而不是 404?
您的服务器端路由似乎需要一些配置。当您点击 http://oktafox.com 时,您的服务器正在发送回索引页面,然后 Angular 启动并设置路由。这就是为什么点击链接在那之后起作用的原因。
但是如果您直接转到 http://oktafox.com/articles,您的服务器正在尝试查找该文档,但它不存在。必须告知您的服务器始终发回索引页,以便 Angular 可以从 URL 读取“/articles”并继续正确路由。
请注意,如果您在 Angular 中切换到基于哈希的路由(例如 http://oktafox.com/#/articles) you may be able to avoid the issue altogether, since the server doesn't see that portion of the route. But it is not recommended: https://angular.io/docs/ts/latest/guide/router.html
Frank Modica 的上述回答一针见血。经过一番挖掘后,我发现这是我的 Nginx 服务器的配置问题。