angular 在 Cloud Foundry 中部署时出现 4 个应用程序路由问题

angular 4 application routing issues when deployed in cloud foundry

我这里有一个独特的路由问题,我有一个 angular 应用程序在我的本地运行良好,但是当应用程序部署到 cloud foundry 时,路由根本不起作用(它给404).

这里是路由的代码片段

import  { LocationStrategy,  PathLocationStrategy }  from  '@angular/common';
const  appRoutes:  Routes  =   [{
  path:   '',
  component:  PageNotFoundComponent
 },
 {
  path:   'contents',
  component:  ContentsComponent
 },
 {
  path:   'sidebar',
  component:  SidebarComponent
 }
];

我正在寻找这些:

路由条件:

  1. 没有散列的路由。
  2. 直接在浏览器中输入 URL 应该会导航到该页面。
  3. 不应显示 404 错误。

有什么建议吗?

import  { LocationStrategy,  PathLocationStrategy }  from  '@angular/common';
const  appRoutes:  Routes  =   [{
  path:   '',
  redirectTo:'contents',
  pathMatch:'full'
 },
 {
  path:   'contents',
  component:  ContentsComponent
 },
 {
  path:   'sidebar',
  component:  SidebarComponent
 },
 {
  path:'**',
  component:PageNotFoundComponent
];

对于部署问题转到 https://angular.io/guide/deployment

您将根据您的服务器获得解决方案

或使用节点服务器部署

解决方案是使用带有路由条件的 .HTACCESS 文件。 在将应用程序部署到 Cloud Foundry

时使用“php_buildpack”作为构建包

.htaccess 文件中使用的路由条件

`RewriteEngine 开启

RewriteCond %{REQUEST_FILENAME} !-f

RewriteCond %{REQUEST_FILENAME} !-d

不重写css、js和图片

RewriteCond %{REQUEST_URI} !.(?:css|js|map|jpe?g|gif|png)$ [NC]

RewriteRule ^(.*)$ /index.html?path=$1 [NC,L,QSA]`