user6123723 user6123723 - 3 months ago 134
TypeScript Question

How to bootstrap with HashLocationStrategy in Angular 2 RC5

I'm upgrading from Angular 2 RC4 to RC5

Here's my current main.ts

import {enableProdMode} from '@angular/core';
import {bootstrap} from '@angular/platform-browser-dynamic';
import {AppComponent} from './app/app.component';
import {AppRoutes} from './app/app.routes';
import { provideRouter } from '@angular/router';
import { XHRBackend } from '@angular/http';
import { HTTP_PROVIDERS } from '@angular/http';
import { LocationStrategy,
HashLocationStrategy } from '@angular/common';
import {disableDeprecatedForms, provideForms} from '@angular/forms';
import {provide} from '@angular/core';

enableProdMode();
bootstrap(AppComponent, [
disableDeprecatedForms(),
provideForms(),
provideRouter(AppRoutes)
,HTTP_PROVIDERS,
provide(LocationStrategy, {useClass: HashLocationStrategy})

])
.catch(err => console.error(err));


Here's my updated main.ts

import { platformBrowserDynamic } from '@angular/platform-browser-dynamic';
import {AppModule} from './app/app.module';

platformBrowserDynamic().bootstrapModule(AppModule);


Here's the app.modules.ts

import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { FormsModule } from '@angular/forms';
import {HTTP_PROVIDERS} from '@angular/http';
import { AppComponent } from './app.component';
import { routing } from './app.routes';

@NgModule({
imports: [
BrowserModule,
FormsModule,
routing
],
declarations: [
AppComponent
],
bootstrap: [ AppComponent]
})
export class AppModule {}


How can I use HashLocationStrategy with RC5? How can I enable Production mode?

Answer

You may use below,

routing

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

for enabling production mode, before loading root NgModule,

import { enableProdMode } from '@angular/core';

if (<condition to enable production mode>) {
   enableProdMode();
}

Hope this helps!!