Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 600
user6123723 Avatar asked Aug 15 '16 23:08

user6123723


1 Answers

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();
}

Read more about LocationStrategy and browser URL styles here.

like image 70
Madhu Ranjan Avatar answered Oct 11 '22 07:10

Madhu Ranjan