【问题标题】:Angular2 - APP_BASE_HREF with HashLocationStrategyAngular2 - 带有 HashLocationStrategy 的 APP_BASE_HREF
【发布时间】:2018-06-13 17:04:13
【问题描述】:

我有一个 Angular 应用程序使用 HashLocationStrategy 路由,我需要在主 html 文件中设置不同的值并在路由中设置不同的值。

我试过这个解决方案:

@NgModule({
    imports: [
        BrowserModule,
        FormsModule,
        HttpModule,
        MyRouting // with useHash set to true
    ],
    declarations: [
        AppComponent,
    ],
    providers: [
        { provide: APP_BASE_HREF, useValue: '/prefix' }
    ],
    bootstrap: [AppComponent]
})
export class AppModule { }

几乎可以正常工作,但值 '/prefix' 在哈希之后插入,如下所示:

http://myapp.com/#/prefix/home

而我想要:

http://myapp.com/prefix/#/home

为了清楚起见,我的基本标签是:

<base href="/">

【问题讨论】:

    标签: angular angular-routing


    【解决方案1】:

    我遇到了同样的问题,并用我自己的 HashLocationStrategy 子类修复了它

    import { Injectable } from '@angular/core';
    import { HashLocationStrategy } from '@angular/common';
    
    @Injectable()    
    export class CustomLocationStrategy extends HashLocationStrategy {
        prepareExternalUrl(internal: string): string {
            return this.getBaseHref() + '#' + internal;
        }
    }
    

    然后在我的模块中使用它

    import { NgModule } from '@angular/core';
    import { RouterModule, Routes } from '@angular/router';
    import { LocationStrategy } from '@angular/common';
    import { APP_BASE_HREF } from '@angular/common';
    import { CustomLocationStrategy } from './app.common';
    
    const appRoutes: Routes = [...];
    
    @NgModule({
        imports: [
            RouterModule.forRoot(appRoutes, { useHash: true })
        ],
        providers: [
            { provide: APP_BASE_HREF, useValue: window.location.pathname },
            { provide: LocationStrategy, useClass: CustomLocationStrategy },
        ]
    })
    export class AppModule {
    }
    

    【讨论】:

    • 就是这样!但是当你使用 HashLocationStrategy 时,你不必为 routerModule 使用选项 useHash。
    • @Jarek 好的,谢谢。我在角度打开了一个问题,所以将来可能会有一个核心选​​项github.com/angular/angular/issues/13482
    【解决方案2】:

    啊啊啊啊啊啊!!让它工作。

    在您的 index.html 文件中,将 baseurl 指定为“.” 像这样:

    <base href=".">
    

    并在NgModule 装饰器中的providers 属性中指定哈希定位策略app.module.ts,如下所示:

    @NgModule({
      declarations: [AppComponent],
      imports: [
        FormsModule,
        HttpModule,
        AppRoutingModule,
        ShellModule,
        ShellProvidersModule,
        BrowserModule
      ],
      providers: [
        SessionService,
        { provide: LocationStrategy, useClass: HashLocationStrategy },
      ],
      bootstrap: [AppComponent]
    })
    export class AppModule { }
    

    见:https://github.com/datumgeek/plotter-app-seed-angular2/blob/master/src/app/app.module.ts#L26

    在这里运行演示: https://datumgeek.github.io/plotter-app-seed-angular2/#/shell;stateRepositoryId=file-host-01;sessionId=session-03

    【讨论】:

    • 这在这种情况下不起作用,这完全是关于如何在 url 中添加散列。我发现的解决方案是创建一个继承自 HashLocationStrategy 的类并覆盖添加哈希的方法,然后将其用作 LocationStrategy 提供者。
    • hmmm...你点击“运行演示链接”了吗?它对我有用...datumgeek.github.io/plotter-app-seed-angular2/#/…
    • 是的,我看到你的演示有效。但这不是我的情况的解决方案。查看 Volker Andres 代码,它是关于更改 url 中的哈希位置。
    • 我没有 Index.html。我的项目在 MVC5 中,角度为 4。我应该在哪里写这个?在 Home/Index.cshtml 还是 Layout.cshtml?
    猜你喜欢
    • 2016-06-07
    • 2017-10-18
    • 1970-01-01
    • 2017-06-10
    • 1970-01-01
    • 2017-01-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多