角路由在URL地址栏上不起作用

时间:2018-12-26 13:31:29

标签: amazon-ec2 deployment angular6 angular-routing

我用路由组件制作了一个Angular项目。而且,我已经处理了一些URL处理,例如,如果用户手动点击了URL:如果URL存在,则转到 app-routing-module.ts 中定义的URL组件,以及该URL不存在,它将显示错误页面,如代码PagenotFoundComponent中所定义。 例如

import { NgModule } from '@angular/core';
import { Routes, RouterModule } from '@angular/router';

import {HomeComponent} from './component/home.component';
import {AboutusComponent} from './component/aboutus.component';
import {SupportComponent} from './component/support.component';
import {PurchaseComponent} from './component/purchase.component';
import {PagenotfoundComponent} from './component/pagenotfound.component';
import {HowitworksComponent} from './component/howitworks.component';
import {ProductComponent} from './component/product.component';
import { ProductVarientDetailsComponent } from './component/product-varient-details.component';

const routes: Routes = [
  { path: '', redirectTo: '/home', pathMatch: 'full'},
  { path: 'home', component: HomeComponent},
  { path: 'about-us', component: AboutusComponent},
  { path: 'how-it-works', component: HowitworksComponent},
  { path: 'support', component: SupportComponent},
  { path: 'purchase', component: PurchaseComponent},
  { path: 'product/:name/details', component: ProductComponent},
  { path: 'product-variants', component: ProductVarientDetailsComponent},
  { path: '**', component: PagenotfoundComponent}
];

@NgModule({
  imports: [RouterModule.forRoot(routes)],
  exports: [RouterModule]
})
export class AppRoutingModule { }
export const routingComponents = [HomeComponent, AboutusComponent, SupportComponent,
                                  PurchaseComponent, PagenotfoundComponent, HowitworksComponent,
                                  ProductComponent, ProductVarientDetailsComponent];

因此,当我手动点击localhost:4200 / home时,它显示了HomeComponent页面;如果执行此localhost:4200 / sdnbgbdfgbh,则显示了本地服务器上的PagenotfoundComponent页面。

然后我转到此链接:https://angular.io/guide/deployment部署我的角度应用程序。我按照文档的步骤进行操作。说。

我的应用程序现在可以完全正常工作,但是当我输入一些手动URL时,它向我显示Apache Server Page Not Found。只是这件事在Angular App中不起作用。

我已将我的应用程序部署在端口80上Apache服务器上的AWS EC2上。

1 个答案:

答案 0 :(得分:1)

尝试像这样重定向到路径404

const routes: Routes = [
   ...otherRoutes
   { path: '404', component: PagenotfoundComponent},
   { path: '**', redirectTo: '404'}
];

如果不起作用,请尝试根据Angular Documentation配置.htaccess:

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

请记住,所有路径匹配都应在apache配置中通过index.html

或者尝试一下这份文档,希望对您有所帮助:

https://github.com/mgechev/angular-seed/wiki/Deploying-prod-build-to-Apache-2