Angular 4 路由不适用于实时网络应用程序
Angular 4 Routing not working on live web-app
我的 Angular 4 网络应用程序路由在我的开发环境中工作正常,菜单重定向在实时版本中工作正常。
但是,开发版通过在浏览器的地址字段中键入内容重定向到不同的页面,而实时版则不会。这是 Angular 问题吗?还是我需要通过 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>
我的实时网络应用程序没有重现这种隐藏的路由器行为吗?我该如何更改它?
我在 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>
您在这里遇到的问题与单页应用程序框架的性质有关,例如 Angular。
在您的应用程序的已部署版本中,托管它的 Web 服务器只知道如何提供它看到的 html 文件 (index.html),该文件对应于您的根路径。第二次尝试直接访问 http://url-to-your-app/art 例如,服务器将抛出 404 not found,因为它无法识别它可以提供的资源路径。
当从应用程序内部导航您的应用程序时,Angular 的路由服务在客户端管理导航;托管网络服务器实际上并没有收到为您的 index.html 以外的其他网页提供服务的请求。
此外,这不会发生在开发上,因为您开发的网络服务器知道如何管理它。
您有两个选择:
- 将您的生产 Web 服务器配置为始终响应
index.html 每当它检测到 404 - 这样,Angular 将始终加载并且其路由服务将处理客户端的导航。
- 将您应用的 locationStrategy 更改为所述的哈希位置策略
here。
但是,这会更改您应用的网址,但事实并非如此
我认为可取。
RouterModule.forRoot(routes, {useHash: true})
在此处查看 hashLocationStrategies 用法:
https://codecraft.tv/courses/angular/routing/routing-strategies/#_hashlocationstrategy
我的 Angular 4 网络应用程序路由在我的开发环境中工作正常,菜单重定向在实时版本中工作正常。
但是,开发版通过在浏览器的地址字段中键入内容重定向到不同的页面,而实时版则不会。这是 Angular 问题吗?还是我需要通过 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>
我的实时网络应用程序没有重现这种隐藏的路由器行为吗?我该如何更改它?
我在 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>
您在这里遇到的问题与单页应用程序框架的性质有关,例如 Angular。
在您的应用程序的已部署版本中,托管它的 Web 服务器只知道如何提供它看到的 html 文件 (index.html),该文件对应于您的根路径。第二次尝试直接访问 http://url-to-your-app/art 例如,服务器将抛出 404 not found,因为它无法识别它可以提供的资源路径。
当从应用程序内部导航您的应用程序时,Angular 的路由服务在客户端管理导航;托管网络服务器实际上并没有收到为您的 index.html 以外的其他网页提供服务的请求。 此外,这不会发生在开发上,因为您开发的网络服务器知道如何管理它。
您有两个选择:
- 将您的生产 Web 服务器配置为始终响应 index.html 每当它检测到 404 - 这样,Angular 将始终加载并且其路由服务将处理客户端的导航。
- 将您应用的 locationStrategy 更改为所述的哈希位置策略 here。 但是,这会更改您应用的网址,但事实并非如此 我认为可取。
RouterModule.forRoot(routes, {useHash: true})
在此处查看 hashLocationStrategies 用法: https://codecraft.tv/courses/angular/routing/routing-strategies/#_hashlocationstrategy