0

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

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

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

知道我可能会错过什么吗?

4

1 回答 1

2

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

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

示例阿帕奇:

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 文档中的更多详细信息。

AWS 更新:

AWS Amplify 默认重定向到index.html发生 404 错误的时间,但这是一个简单的重定向。从文档

大多数 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
于 2020-03-14T12:54:55.893 回答