【发布时间】:2016-04-21 08:08:55
【问题描述】:
我正在关注 Angular 2 路由示例。使用“lite”网络服务器,我可以从根目录导航,并且深度链接可以工作,但是使用 Apache,我可以从根目录导航,但是在直接链接到路由时会出现 404 Not Found 错误。
例如,以下 URL 适用于由 npm 在端口 3000 上启动的“lite”网络服务器。
http://localhost:3000/crisis-center/2
但是下一个针对 Apache 在端口 80 上运行的 URL 失败。
http://localhost/crisis-center/2
The requested URL /crisis-center/2 was not found on this server.
我确实尝试了一些针对类似 Angular 1 问题推荐的 .htaccess 解决方案,但没有成功。如果有人在 Apache 上进行过 Angular 2 路由和深度链接工作,请告诉我您是如何实现的。
@RouteConfig([
{ // Crisis Center child route
path: '/crisis-center/...',
name: 'CrisisCenter',
component: CrisisCenterComponent,
useAsDefault: true
},
{path: '/heroes', name: 'Heroes', component: HeroListComponent},
{path: '/hero/:id', name: 'HeroDetail', component: HeroDetailComponent},
{path: '/disaster', name: 'Asteroid', redirectTo: ['CrisisCenter', 'CrisisDetail', {id:3}]}
])
Boot.ts
import {bootstrap} from 'angular2/platform/browser';
import {ROUTER_PROVIDERS} from 'angular2/router';
import {AppComponent} from './app.component';
bootstrap(AppComponent, [ROUTER_PROVIDERS]);
【问题讨论】:
-
它可以与angular.io/docs/js/latest/api/router/… 一起使用吗?
-
我确实设法让 PathLocationStrategy 在 Apache 上运行以下 .htaccess 我之前的尝试失败了,因为 AllowOverride 和 mod_rewrite 模块配置存在问题。 RewriteEngine On # 如果请求现有资产或目录,则按原样转到它 RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} -f [OR] RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} -d RewriteRule ^ - [L] # If请求的资源不存在,使用 index.html RewriteRule ^ /index.html