【问题标题】:Angular 7 Routes not working when deployed to Apache 2 HTTPD ServerAngular 7 路由在部署到 Apache 2 HTTPD 服务器时不起作用
【发布时间】:2019-08-08 10:03:02
【问题描述】:


我一直在尝试将我的 Angular 7 应用程序部署到 HTTPD Apache 2 服务器。 这些是我采取的步骤:

  1. 为我的项目设置虚拟主机
  2. 构建 --> ng build --prod
  3. 按照here的指示创建和配置.htaccess:

     <IfModule mod_rewrite.c>
      RewriteEngine On
      RewriteBase /
      RewriteRule ^index\.html$ - [L]
      RewriteCond %{REQUEST_FILENAME} !-f
      RewriteCond %{REQUEST_FILENAME} !-d
      RewriteRule . /index.html [L]
    

  4. 像这样配置我的 app-routing.module.ts:

导入:[RouterModule.forRoot(routes ,{onSameUrlNavigation: 'reload'})]

我的主页可以正常工作,但是当我尝试浏览其他路线时,例如http://myhostname/users/crud,它不再起作用了。 我在这里错过了什么吗?

谢谢。

【问题讨论】:

  • 很可能你需要在你的 Angular 应用中设置base href
  • 你有没有看过这个问题:stackoverflow.com/questions/34816025/…
  • 谢谢@Wandrille。但我放弃了这一点,因为我现在不是一个活跃的 PHP 开发人员。我尝试了您的答案,但我使用了带有嵌入式 apache tomcat 的 Springboot,并且我通过 Zuul 和 application.properties 处理了路由。而且效果很好:)!

标签: angular apache


【解决方案1】:
Build --> ng build --prod --baseHref=/users/crud/

而不是

Build --> ng build --prod

试试上面的代码。该代码对我有用。

【讨论】:

  • 谢谢@user2455218。但我放弃了这个。我尝试了你的答案,但我使用了带有嵌入式 apache tomcat 的 Springboot,我通过 Zuul 和 application.properties 处理了路由。而且效果很好!
猜你喜欢
  • 2017-09-18
  • 1970-01-01
  • 2022-12-15
  • 2017-08-19
  • 1970-01-01
  • 2022-08-23
  • 2015-02-08
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多