【问题标题】:Angular 8 app: window.open in new window always redirects to index.htmlAngular 8 应用程序:window.open 在新窗口中总是重定向到 index.html
【发布时间】:2020-03-14 11:46:52
【问题描述】:

我已经使用一个标签设置了我的应用程序,该标签附加了一个指令,该指令将调用 window.open 希望我的应用程序在我的路线路径的新窗口中打开

window.open(
      '/my-path',
      '_blank',
      'height=550,width=1400'
    );

这一切在 localhost 中运行良好,但由于某种原因,当我进入 dev 时,新窗口总是希望重定向到 index.html 并且只是将我弹回根目录,而不是转到 /my-path。

知道我可能缺少什么吗?

【问题讨论】:

  • 您的开发 URL 路径是否与本地不同?例如:本地:localhost:4200/my-path 和开发:your-domain.tld/webapp/my-path ?
  • @AndréRoggeriCampos,dev和本地路径完全一样

标签: angular amazon-web-services angular-ui-router aws-amplify window.open


【解决方案1】:

我猜(因为问题可能有多种原因......)您的服务器尝试处理对子路由的请求,但这应该由 Angular 在客户端完成。

您必须将服务器配置为针对所有请求回退到 index.html

示例 Apache:

RewriteEngine On
# If an existing asset or directory is requested go to it as it is
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} -f [OR]
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} -d
RewriteRule ^ - [L]
# If the requested resource doesn't exist, use index.html
RewriteRule ^ /index.html

Nginx 示例:

try_files $uri $uri/ /index.html;

更多详情请关注Angular documentation

AWS 更新:

当发生 404 错误时,AWS Amplify 默认重定向到 index.html,但这是一个简单的重定向。来自docs

大多数 SPA 框架都支持 HTML5 history.pushState() 来更改浏览器位置而不触发服务器请求。这适用于从根目录(或 /index.html)开始旅程的用户,但不适用于直接导航到任何其他页面的用户。使用正则表达式,以下示例为 index.html 的所有文件设置 200 次重写,正则表达式中指定的特定文件扩展名除外。

以下设置应适用于 AWS Amplify:

// Original address
</^[^.]+$|\.(?!(css|gif|ico|jpg|js|png|txt|svg|woff|ttf|map|json)$)([^.]+$)/>

// destination
/index.html

// Redirect Type
200

【讨论】:

  • 是的,我想就是这样......我的 Angular 应用程序通过 AWS amplify 部署(它使用 Cloudfront),帮助其他人进行重定向(在这种情况下为 404): docs.aws.amazon.com/amplify/latest/userguide/redirects.html
  • 实际上我确实(默认情况下在 Amplify 中)重定向到 404 上的 index.html ......我可以看到这个重定向到 index.html 路径正在发生,但是路由不会继续“ /我的路径“
  • 是的,但这是一个简单的重定向,没有重写。我仍然认为我对问题的原因是正确的。请参阅我的更新答案。
  • 您找到问题的原因了吗?如果是这样,介意发布答案吗?
  • 是的,是把404重定向改成rewrite的情况,谢谢!
猜你喜欢
  • 1970-01-01
  • 2015-06-11
  • 1970-01-01
  • 2010-09-11
  • 2020-04-05
  • 2011-06-24
  • 1970-01-01
  • 2016-05-29
  • 1970-01-01
相关资源
最近更新 更多