Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hash location strategy not working -- Angular 4 -- NgxChartsModule broken the hash location strategy

I'm developing an Angular 4 application. I intended to use the HashLocationStrategy but its not working(# is not appearing in the URLs). Below is my app routing module:

import { NgModule } from '@angular/core';
import { RouterModule, Routes } from '@angular/router';

import { DashboardPage } from './dashboard-page/component';
import { AccountPage } from './acct-page/component';

const appRoutes: Routes = [
    { path: 'dashboard', component: DashboardPage },
    { path: 'accounts/:number', component: AccountPage},
    { path: '', redirectTo: '/dashboard', pathMatch: 'full' },
];

@NgModule({
    imports: [RouterModule.forRoot(appRoutes, { enableTracing: false, useHash: true })],
    exports: [RouterModule],
    providers: []
})
export class AppRoutingModule { }

Below is my app.module.ts:

import { BrowserModule } from '@angular/platform-browser';
import { RouterModule, Routes } from '@angular/router';
import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common'
import { FormsModule, ReactiveFormsModule } from '@angular/forms';
import { BrowserAnimationsModule } from '@angular/platform-browser/animations';
import { HttpClientModule } from '@angular/common/http';
import { NgxChartsModule } from '@swimlane/ngx-charts';
import { DataTableModule } from "angular2-datatable";
import {  
  MatToolbarModule, MatToolbarRow, MatIconModule, MatSidenavModule, MatListModule, MatButtonModule, 
  MatCardModule, MatMenuModule, MatGridListModule, MatInputModule, MatDatepickerModule, MatNativeDateModule,
  MatTableModule, MatTabsModule, MatCheckboxModule
} from '@angular/material';
import { MatSelectModule } from '@angular/material/select';
import {MatExpansionModule} from '@angular/material/expansion';

import { AppComponent } from './app-component/app.component';
import { AppRoutingModule } from './app-routing.module';

import { DashboardPage } from './dashboard-page/component';
import { AccountPage } from './acct-page/component';


@NgModule({
    declarations: [
        AppComponent, DashboardPage, BreadcrumbComponent, AccountPage
    ],
    imports: [
        AppRoutingModule, RouterModule, BrowserModule, HttpClientModule, BrowserAnimationsModule, MatToolbarModule, MatIconModule,
        MatSidenavModule, MatListModule, MatButtonModule, MatCardModule, MatInputModule, CommonModule,
        MatMenuModule, MatGridListModule, DataTableModule, FormsModule, ReactiveFormsModule, MatDatepickerModule, 
        MatNativeDateModule, MatTableModule, MatTabsModule, MatExpansionModule, MatCheckboxModule, MatSelectModule, NgxChartsModule
    ],
    providers: [],
    bootstrap: [AppComponent],
    entryComponents: [XMLContentDialog]
})
export class AppModule {
}

I used RouterModule.forRoot(appRoutes, { enableTracing: false, useHash: true }) for enabling the hash location strategy, but its not working. Am I missing something here?

[EDIT]

Updated 'title' to reflect the root cause

like image 217
Nagendra Varma Avatar asked Jan 04 '18 06:01

Nagendra Varma


People also ask

What is hash location strategy in Angular?

HashLocationStrategylink. A LocationStrategy used to configure the Location service to represent its state in the hash fragment of the browser's URL.

How many strategies are used to enable the location service to read route state from the browser's URL?

LocationStrategylink Enables the Location service to read route state from the browser's URL. Angular provides two strategies: HashLocationStrategy and PathLocationStrategy .

When true enable the location strategy that uses the URL fragment instead of the History API?

//useHash enables the location strategy that uses the URL fragment instead of the history API. ], The HashLocationStrategy add the route path to the hash (#) in the browser's URL. The hash (#) part of the URL is called the hash fragment.

Which of the below code is used to import hash location strategy?

To switch to HashLocationStrategy, use this code: imports: [ ... RouterModule. forRoot(routes, { useHash: true }), ... ]


3 Answers

It turns out that this behavior only occurs when we include NgxChartsModule after including the App's RoutingModule that imports the RouterModule. Once I moved the import of NgxChartsModule before the AppRoutingModule, it seems to work fine!

[CREDIT] https://github.com/swimlane/ngx-charts/issues/601

like image 73
Nagendra Varma Avatar answered Nov 17 '22 20:11

Nagendra Varma


Apparently, when you lazy-load modules, Imports order matters! (Shocked as you!)

move - AppRoutingModule to the bottom and you've got yourself a solution.

i.e.

imports: [
    // ... all app modules...
   , AppRoutingModule
],
like image 22
Artipixel Avatar answered Nov 17 '22 19:11

Artipixel


int your app.module.ts

import { CommonModule, HashLocationStrategy, LocationStrategy } from '@angular/common';
import { RouteReuseStrategy } from '@angular/router'
...
 providers: [
    { provide: LocationStrategy, useClass: HashLocationStrategy },...]
like image 29
Eliseo Avatar answered Nov 17 '22 18:11

Eliseo