angular - Angular 6 - 在不使用直接链接的路径下发布
问题描述
我有一个 Angular 6 应用程序,在开发过程中运行良好。导航到不同的路线效果很好。当我使用 ng serve -o 时,我可以打开浏览器并转到http://localhost:4200工作正常......如果在主页中单击登录,我将转到http://localhost:4200/login。 .. 都好。即使我不单击登录按钮,而是在浏览器中输入http://localhost:4200/login或任何其他路线,它也可以正常工作。
现在我必须在路径下发布这个应用程序:http://example.com/mypath
. 我用这个命令构建它:ng build --prod --build-optimizer --base-href=/mypath/
,把它放到服务器上,如果我使用应用程序中的链接导航到不同的路线,一切都很好。
问题是,如果我在浏览器中输入直接 url,例如:http://example.com/mypath/login
,那么它就不起作用。
我怎么解决这个问题?
解决方案
在 .htaccess 中为 apache 添加此配置或为路由添加 useHash 属性
RewriteEngine On
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} -f [OR]
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} -d
RewriteRule ^ - [L]
RewriteRule ^ /index.html
推荐阅读
- amazon-web-services - AWS SAM rest api 网关无法正确处理自定义域中的“-”
- java - 在 PrimeFaces 图表中自定义 PointLabels
- javascript - ChartJS 在 IE11 中延长了 50 年
- angular - 如何将管道发送到组件
- wordpress - 针对特定域的 WordPress 多站点和 301 重定向
- python - any() 的返回值
- python - 编写纸浆约束时遇到问题
- pagination - ngx-owl-carousle-o 分页在 Angular 10 中不起作用
- python - 如何获取数据框行以列出索引号?
- reactjs - 使用 jest 和 redux-mock-store 测试 redux 连接的组件