【问题标题】:Angular2 rc.5 and hashbangAngular2 rc.5 和 hashbang
【发布时间】:2016-12-18 13:29:09
【问题描述】:

在之前的 rc.4 版本中设置 hashbang #!看起来像这样

bootstrap(ApplicationComponent, [....
    {provide: LocationStrategy, useClass: HashLocationStrategy},
    {provide: APP_BASE_HREF, useValue: '!'}
]);

现在在 rc.5 中,一切都发生了一点变化,我很难找到一种方法来让相同的地址包含 #!

Angular 文档对设置路由非常清楚,但在描述中略去了 hashbang(https://angular.io/docs/ts/latest/guide/router.html)。单独设置哈希就像在 rc.5 app.routing.ts 中那样完成

export const routing = RouterModule.forRoot(appRoutes, { useHash: true });

但不确定如何另外定义“!”在 rc.5 的 url 中

我不确定它应该如何应用以及以什么形式应用。也许在 boostrap:... 在 app.module 中的@NgModule 中,但到目前为止我还没有让它工作。请提出建议。

【问题讨论】:

    标签: angular angular2-routing hashbang


    【解决方案1】:

    您可以在app.module.ts 文件中这样做,方法是将其添加到providers 数组中

    import {ReflectiveInjector} from '@angular/core';
    import { Http, Response, Request, HTTP_PROVIDERS } from '@angular/http';
    
    let injector = ReflectiveInjector.resolveAndCreate(HTTP_PROVIDERS);
    let http = injector.get(Http);
    let authService = new AuthService(new LocalStorage(), http);
    
    @NgModule({
      imports: [
        BrowserModule,
        FormsModule,
        routing,
    
      ],
      declarations: [
        AppComponent,
        MyComponent,
      ],
      bootstrap: [
        AppComponent
      ],
      providers: [
        {provide:AuthService,useValue:authService}, // over here
        AdminAuthGuard,
        UserAuthGuard
      ]
    })
    

    【讨论】:

    • 非常感谢。我以为我已经尝试过了,但又试了一次,一切都很好。谢谢。
    猜你喜欢
    • 2016-12-28
    • 2016-12-20
    • 2016-09-15
    • 2016-10-04
    • 2016-12-14
    • 1970-01-01
    • 2017-01-12
    • 1970-01-01
    • 2017-01-15
    相关资源
    最近更新 更多