我猜(因为问题可能有各种原因......)您的服务器尝试处理对子路由的请求,但这应该由 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