【问题标题】:Angular2 routing in IIS - 404 on refresh - How to use HashLocationStrategyIIS 中的 Angular2 路由 - 刷新时出现 404 - 如何使用 HashLocationStrategy
【发布时间】:2017-08-29 12:15:25
【问题描述】:

当我启动我的应用程序 (localhost/) 时,一切正常并重定向到默认路由 (localhost/welcome)。但是,当我重新加载页面时,IIS 正在寻找一个不存在的目录 Welcome,因为它只是一个虚拟路由。

我读到有两种解决方案。 1. IIS Url 重写,2. 使用 HashLocationStrategy(不知道为什么以及如何解决问题)。

很遗憾,我无法让解决方案 2 起作用:

    @NgModule({
    imports:      [ MapModule, BrowserModule, HttpModule, FormsModule, routing, DataTableModule, MdGridListModule, GrowlModule, TabMenuModule, PanelModule],
    declarations: [ Application, CellDetailComponent, WelcomeComponent, SlimLoadingBarComponent],
    bootstrap:    [ Application ],
    providers: [
        appRoutingProviders,
        AppConfig,
        { provide: APP_INITIALIZER, useFactory: (config: AppConfig) => () => config.load(), deps: [AppConfig], multi: true },
        { provide: LocationStrategy, useClass: HashLocationStrategy}
    ]
})

路线设置:

import { Routes, RouterModule } from '@angular/router';
import {CellDetailComponent} from "./components/infopanel/cell-detail.component";
import {WelcomeComponent} from "./components/infopanel/welcome.component";
import { ModuleWithProviders } from '@angular/core';

const appRoutes: Routes = [
    { path: 'welcome', component: WelcomeComponent },
    { path: 'cell/:id', component: CellDetailComponent },
    { path: '',  redirectTo: '/welcome', pathMatch: 'full'},        // default route
    { path: '**', component: WelcomeComponent }                     // PathNotFound route
];

export const appRoutingProviders: any[] = [

];

export const routing: ModuleWithProviders = RouterModule.forRoot(appRoutes);

我做错了什么?

【问题讨论】:

标签: angular iis


【解决方案1】:

刚刚找到答案:

    RouterModule.forRoot(routes, { useHash: true })

【讨论】:

    【解决方案2】:

    在 Angular 4 的最新版本中,我们没有单独的引导程序[AppComponent] 你只需要写:

    RouterModule.forRoot(routes, **{ useHash: true }**)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-10-12
      • 1970-01-01
      • 2019-07-02
      • 1970-01-01
      • 2019-05-19
      • 2017-04-17
      • 2022-12-09
      相关资源
      最近更新 更多