首页 > 解决方案 > 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,那么它就不起作用。

我怎么解决这个问题?

标签: angularangular6angular-routing

解决方案


在 .htaccess 中为 apache 添加此配置或为路由添加 useHash 属性

RewriteEngine On
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} -f [OR]
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} -d
RewriteRule ^ - [L]

RewriteRule ^ /index.html

推荐阅读