Angular 4 Routing不适用于实时网络应用程序

Dav*_*983 6 routing url-routing typescript angular

我的Angular 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);
Run Code Online (Sandbox Code Playgroud)

在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 { }
Run Code Online (Sandbox Code Playgroud)

我不明白forRoot在做什么,或者在Angular 4中使用它是否合适?

我的app.component.html如下,并使用隐藏的路由器插座:

<body>
<app-mf-toolbar></app-mf-toolbar>
<router-outlet class="hidden-router"></router-outlet>
</body>
Run Code Online (Sandbox Code Playgroud)

这是我的实时网络应用程序无法再现的隐藏路由器行为,我该如何更改?

我在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>
Run Code Online (Sandbox Code Playgroud)

And*_*aru 17

您在这里遇到的问题与单页应用程序框架的性质有关,例如Angular.

在部署的应用程序版本上,托管它的Web服务器只知道如何提供它看到的一个html文件(index.html),它对应于您的根路径.第二个你尝试直接访问http:// url-to-your-app/art,服务器会抛出404找不到,因为它不能识别它可以服务的资源的路径.

从应用程序本身浏览应用程序时,它是Angular的路由服务,用于管理客户端的导航; 主机Web服务器实际上不会收到服务其他网页的请求,而不是index.html.此外,这不会发生在开发人员身上,因为您的开发人员服务器知道如何管理它.

您有两种选择:

  1. 将生产Web服务器配置为每当检测到404时始终使用index.html进行响应 - 这样,Angular将始终加载,其路由服务将处理客户端上的导航.
  2. 所描述的更改应用程序的locationStrategy到哈希定位策略 在这里.但是,这会改变您应用的网址,在我看来并不是那么令人满意.

  • 您并不是真的了解我 - 您需要配置您的Web服务器(如IIS,nginx,apache,无论您使用什么),以便在收到请求时返回index.html,该请求将导致发送404 HTTP响应背部.您无需对Angular路由配置执行任何操作. (3认同)
  • @Alex,查看我对类似问题的其他答案[这里](/sf/answers/3209090601/) (3认同)