【问题标题】:Angular Routing not working on URL Address barAngular Routing 在 URL 地址栏上不起作用
【发布时间】:2018-12-26 13:31:29
【问题描述】:

我制作了一个带有路由组件的 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 Page,如果我这样做 localhost:4200/sdnbgbdfgbh,它会显示本地服务器上的 PagenotfoundComponent Page。

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

我的应用程序现在可以完全运行,但是当我手动输入一些 url 时,它会显示 Apache Server Page Not Found。只是这个东西在 Angular 应用程序中不起作用。

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

【问题讨论】:

  • 你能用 .htaccess 内容更新你的问题吗?要清楚地了解需要查看 .htaccess 配置。
  • 我的 .htaccess 文件是空的
  • 我在 .htaccess 文件中添加了RewriteEngine on RewriteCond %{REQUEST_FILENAME} -s [OR] RewriteCond %{REQUEST_FILENAME} -l [OR] RewriteCond %{REQUEST_FILENAME} -d RewriteRule ^.*$ - [NC,L] RewriteRule ^(.*) /index.html [NC,L],但它仍然无法正常工作。

标签: amazon-ec2 deployment angular6 angular-routing


【解决方案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

【讨论】:

  • 我确实按照您的建议编辑了 app-routing.module.ts 文件并更新了 .htacces 文件。我在“/var/www/html/”文件夹中添加了 web.config 文件,因为 Angular 文档已记录在案。我想尝试 Nginx:使用 try_files,如 Front Controller Pattern Web Apps 中所述,修改为提供 index.html: 实际上我不知道我必须在哪里执行此操作。并且该应用程序仍然无法用于故意点击 URL 或刷新页面。
  • @Mohit ,不要混淆 nginx 和 apache 来服务单个 Angular 应用程序。你能按照这个文件吗? https://github.com/mgechev/angular-seed/wiki/Deploying-prod-build-to-Apache-2
  • 非常感谢。这份文件对我有用。我深陷其中。再次感谢您!
  • 我正在更新答案,如果我帮助了你,请将此答案标记为已解决。谢谢。
  • 嗨@Fahim,我已经按照AWS EC2服务器上的旧实例的步骤进行操作。但现在我转移到新的 AWS EC2 实例,这一次,https://github.com/mgechev/angular-seed/wiki/Deploying-prod-build-to-Apache-2 链接因故意路由而失败。
猜你喜欢
  • 2016-07-13
  • 2013-02-12
  • 1970-01-01
  • 2016-01-01
  • 1970-01-01
  • 2020-01-30
  • 2012-04-14
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多