我開發了一個小型的Angular2測試應用程序,其中包括其他功能,利用路由。Angular 2路由和直接訪問特定路由:如何配置Apache?
只要用戶第一次訪問主頁,然後導航到子頁面,它就可以正常工作。
如果用戶試圖直接訪問子頁面,如果我沒有配置Web服務器,我得到了404。這是完全正常的,因爲沒有與該路線相對應的「真實頁面」。
我嘗試添加在Apache 2.2的下面的配置來處理HTML5按壓狀態:
<Directory /var/www/html/angular2-sens>
Options Indexes FollowSymLinks
RewriteEngine On
RewriteBase /angular2-sens
RewriteRule ^index\.html$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /angular2-sens/index.html [L]
</Directory>
這讓事情變得更好,因爲我不再有一個404。然而,我「只是」重定向到家庭的應用程序和路由不執行。
我該怎麼做才能糾正這個問題?
我的index.html頁:
<html>
<head>
<title>Angular 2 QuickStart</title>
<link rel="stylesheet" href="simplegrid.css" type="text/css">
<link rel="stylesheet" href="sen.css" type="text/css">
<script src="node_modules/angular2/bundles/angular2-polyfills.js"></script>
<script src="node_modules/systemjs/dist/system.src.js"></script>
<script src="node_modules/rxjs/bundles/Rx.js"></script>
<script src="node_modules/angular2/bundles/angular2.js"></script>
<script src="node_modules/angular2/bundles/http.js"></script>
<script src="node_modules/angular2/bundles/router.js"></script>
<!-- dev
<script src="node_modules/angular2/bundles/angular2.dev.js"></script>
<script src="node_modules/angular2/bundles/http.dev.js"></script>
<script src="node_modules/angular2/bundles/router.dev.js"></script>
-->
<!-- 2. Configure SystemJS -->
<script>
System.config({
map: {
rxjs: 'node_modules/rxjs' //or wherever you have it installed
},
packages: {
app: {
format: 'register',
defaultExtension: 'js'
},
rxjs: {
defaultExtension: 'js'
}
}
});
System.import('app/boot')
.then(null, console.error.bind(console));
</script>
</head>
<!-- 3. Display the application -->
<body>
<app>Chargement...</app>
</body>
<script>document.write('<base href="' + document.location + '" />');</script>
</html>
我的應用程序/ boot.ts是:
import {bootstrap} from 'angular2/platform/browser';
import {HTTP_PROVIDERS} from 'angular2/http';
import {AppComponent} from './app.component';
import {ROUTER_PROVIDERS} from 'angular2/router';
import { enableProdMode } from 'angular2/core'; enableProdMode();
bootstrap(AppComponent, [HTTP_PROVIDERS,ROUTER_PROVIDERS]);
最後,我的應用程序組件:
import {bootstrap} from 'angular2/platform/browser';
import {HTTP_PROVIDERS} from 'angular2/http';
import {AppComponent} from './app.component';
import {ROUTER_PROVIDERS} from 'angular2/router';
import { enableProdMode } from 'angular2/core'; enableProdMode();
bootstrap(AppComponent, [HTTP_PROVIDERS,ROUTER_PROVIDERS]);
我配置路由的應用程序組件是:
import {Component} from 'angular2/core';
import {RouteConfig, ROUTER_DIRECTIVES} from 'angular2/router';
import {SensComponent} from './sens.component';
import {GroupesPolitiquesComponent} from './groupes-politiques.component';
import {SenVignetteComponent} from './sen-vignette.component';
@Component({
selector: 'app',
template: `
<h1>Application Angular 2 Relative aux Sénateurs (AA2RSen)</h1>
<nav>
<a [routerLink]="['Senateurs']">Tous les Sénateurs en cours de mandat</a>
<a [routerLink]="['GroupesPolitiques']">Groupes politiques</a>
</nav>
<router-outlet></router-outlet>
`,
directives: [ROUTER_DIRECTIVES]
})
@RouteConfig([
{path:'/senateurs', name: 'Senateurs', component: SensComponent},
{path:'/groupes', name: 'GroupesPolitiques', component: GroupesPolitiquesComponent},
{path:'/senateur/:matricule', name: 'VignetteSenateur', component: SenVignetteComponent}
])
export class AppComponent { }
請檢查http://stackoverflow.com/questions/31415052/angular-2-0-router-not-working-on-reloading-the-browser,尤其是' '標籤。 –