2016-02-04 9 views
14

私は小さなAngular2テストアプリを開発しました。これは、とりわけルーティング機能を活用しています。角度2ルーティングと特定のルートへの直接アクセス:Apacheの設定方法?

ユーザーが最初にホームページにアクセスしてからサブページに移動する限り、正常に機能します。

ユーザーがサブページに直接アクセスしようとすると、Webサーバーを構成しないと404が表示されます。ルートに対応する「実ページ」がないため、これは完全に正常です。

私は、HTML5の状態をプッシュ処理するためのApache 2.2で、次の設定を追加しようとしました:

<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ページがある:

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]); 

そして、最後に、私のアプリのコンポーネントは次のとおりです:

<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 {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 { } 
+3

特に、タグを確認してください。http://stackoverflow.com/questions/31415052/angular-2-0-router-not-working-on-reloading-the-browser –

答えて

6

okだから、動的に生成されたベースhrefが間違っていたという事実を除いて、すべてうまくいきました。私の元のコードではAngular 2.0 router not working on reloading the browser

を指しているため、@のギュンター・zöchbauerへ

おかげで、それははdocument.locationから生成されます。

<script>document.write('<base href="' + document.location + '" />');</script> 

私は、静的な要素に切り替える場合:

<base href="/angular2-sens/"/> 

これは機能します。もちろん、私はむしろ "実際の"アプリでdocument.locationをさらに分析します。

+0

変更前後で生成されたURLがどのように違うのか不思議です。 –

+0

ここで示す解決策では、基本要素は静的であり、もはや生成されません。または私はあなたの質問を理解できませんでしたか? –

+0

これは、前者が正しい 'href'を生成しなかったことではなく、動的に追加された' 'タグがまったく動作しなかったことです。 –

関連する問題