2017-08-21 6 views
4

私の開発環境でAngular 4 web-appルーティングがうまく動作し、ライブバージョンでメニューリダイレクトが正常に機能します。角度4のルーティングがライブウェブアプリケーションで動作しない

しかし、devバージョンはブラウザのアドレスフィールドに入力することによって別のページにリダイレクトされますが、ライブバージョンは表示されません。これは角度の問題ですか?または、ISPでリダイレクトを管理する必要がありますか?次のように

マイapp.router.tsコードは次のとおりです。

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

import { HomeComponent } from './home/home.component'; 
import { ArtComponent } from './art/art.component'; 
import { MusicComponent } from './music/music.component'; 
import { EventsComponent } from './events/events.component'; 

export const router: Routes = [ 
{ path: '', redirectTo: 'home', pathMatch: 'full'}, 
{ path: 'home', component: HomeComponent }, 
{ path: 'art', component: ArtComponent }, 
{ path: 'music', component: MusicComponent }, 
{ path: 'events', component: EventsComponent } 
]; 

export const routes: ModuleWithProviders = RouterModule.forRoot(router); 

そして、私が持っているapp.module.ts中:

import { BrowserModule } from '@angular/platform-browser'; 
import { NgModule, } from '@angular/core'; 
import { RouterModule } from '@angular/router'; 
import { router } from './app.router'; 

import 'hammerjs'; 

import { BrowserAnimationsModule } from '@angular/platform-browser/animations'; 
import { FormsModule } from '@angular/forms'; 
import { MdInputModule, MdButtonModule, MdToolbarModule, MdMenuModule, MdGridListModule, MaterialModule, MdDatepickerModule, MdNativeDateModule } from '@angular/material'; 
import { HttpModule } from '@angular/http'; 

import { AppComponent } from './app.component'; 
import { MfToolbarComponent } from './mf-toolbar/mf-toolbar.component'; 
import { MfMenuComponent } from './mf-menu/mf-menu.component'; 
import { SplashComponent } from './splash/splash.component'; 
import { ArtComponent } from './art/art.component'; 
import { MusicComponent } from './music/music.component'; 
import { EventsComponent } from './events/events.component'; 
import { HomeComponent } from './home/home.component'; 
import { ImageSliderComponent } from './image-slider/image-slider.component'; 

// import { HTTPTestService } from './date-data.service'; 
import { AuthenticationService } from './authentication-service.service'; 

import { DataService } from './data.service'; 
import { SvgViewerComponent } from './svg-viewer/svg-viewer.component'; 
import { CalendarComponent } from './calendar/calendar.component'; 

@NgModule({ 
    declarations: [ 
    AppComponent, 
    MfToolbarComponent, 
    MfMenuComponent, 
    SplashComponent, 
    ArtComponent, 
    MusicComponent, 
    EventsComponent, 
    HomeComponent, 
    ImageSliderComponent, 
    SvgViewerComponent, 
    CalendarComponent 
    ], 
    imports: [ 
    BrowserModule, 
    BrowserAnimationsModule, 
    FormsModule, 
    MdInputModule, 
    MdButtonModule, 
    MdToolbarModule, 
    MdMenuModule, 
    MdDatepickerModule, 
    MdNativeDateModule, 
    HttpModule, 
    RouterModule.forRoot(router), 
    ], 
    providers: [ 
    // [HTTPTestService], 
    [AuthenticationService], 
    [DataService], 
    ], 
    bootstrap: [AppComponent] 
}) 
export class AppModule { } 

私はforRootがやった場合、またはされるかを理解していませんAngular 4でそれを使用するのは適切ですか?

私app.component.htmlは、以下のようで、隠されたルータのコンセントを使用しています。

<body> 
<app-mf-toolbar></app-mf-toolbar> 
<router-outlet class="hidden-router"></router-outlet> 
</body> 

は、それは私のライブのWebアプリが再現されていないとどのように私はこれを変更しないと、この隠されたルータの動作ですか?

は、私はまた、ルータのリンクを使用していますmenu.component.htmlのメニューを持っており、これが正常に動作します:あなたがここに持っている問題のような単一ページのアプリケーションフレームワークの性質に関係しています

<div class="container"> 
    <md-menu #appMenu="mdMenu"> 
     <a routerLink="home"> 
      <button md-menu-item> 
       <md-icon class="material-icons"> home </md-icon> 
       <span> Home </span> 
      </button> 
     </a> 
     <a routerLink="art"> 
      <button md-menu-item> 
       <md-icon class="material-icons"> format_paint </md-icon> 
       <span> Art </span> 
      </button> 
     </a> 
     <a routerLink="music"> 
      <button md-menu-item> 
       <md-icon class="material-icons"> music_note </md-icon> 
       <span> Music </span> 
      </button> 
     </a> 
     <a routerLink="events"> 
      <button md-menu-item> 
       <md-icon class="material-icons"> event_note </md-icon> 
       <span> Events </span> 
      </button> 
     </a> 
    </md-menu> 

    <button md-icon-button [mdMenuTriggerFor]="appMenu" color="secondary"> 
     <i class="material-icons">menu</i> 
    </button> 
</div> 
+0

解決していますか? –

+0

私の彼の答えはここhttps://stackoverflow.com/questions/45862243/angular-router-get-404-bug/48374763#48374763 –

答えて

8

角度。

アプリケーションのデプロイ済みバージョンでは、ホストしているWebサーバーは、表示されるhtmlファイル(index.html)をどのように扱うかを知っています(これはルートパスに相当します)。 2番目に直接http://url-to-your-app/artにアクセスしようとすると、サーバーは、それが提供できるリソースのパスとして認識されないため、404が見つかりません。

アプリケーション内からアプリケーションをナビゲートするときは、クライアント側のナビゲーションを管理するAngularのルーティングサービスです。ホスティングWebサーバーは実際にindex.html以外のWebページを提供する要求を受け取りません。 また、これはdevのWebサーバーがこれを管理する方法を知っているので、devで発生しません。

あなたは2つのオプションがあります。

  1. は、それが404を検出したときに、常に のindex.htmlで応答する本番Webサーバーを設定 - そのように、角度が常にロードされ、そのルーティングサービスは、ナビゲーションを処理しますクライアント側。
  2. hereのように、アプリのlocationStrategyをハッシュロケーション戦略に変更します。 しかし、それはあなたのアプリのURLを変更し、それは私の意見では が望ましいわけではありません。
+0

私はリダイレクトを使ってみましたが、まだ正しくリダイレ​​クトされていません!これは何もしないようです:{path: '404'、redirectTo: 'home'、pathMatch: 'full'}、 {path: '**'、redirectTo: 'home'、pathMatch: 'full'} 、 – Davtho1983

+2

あなたは本当に私を理解していません - あなたは404 HTTP応答になる要求を受け取ったときにindex.htmlを返すようにWebサーバ(IIS、nginx、apacheなど)を設定する必要があります返送される。角度ルーティング設定には何もする必要はありません。 –

+0

ok 1&1ガイドを参考にして.htaccessファイルを作成しましたが、何も動作しません - 多くの人が同じ問題を抱えているようです。助言がありますか? – Davtho1983

関連する問題