user2959870
user2959870

Reputation: 78

HashLocationStrategy not working Angular 2

I am developing an webapp using Angular2. I am trying to implement Routing with HasLocationStratery. My application starts with this URL: https://127.0.0.1:8443/admin

This is how i have configured my Routes:

import {Routes, RouterModule} from '@angular/router';
import {AppComponentAdmin} from "./app.component.admin";
import {ModuleWithProviders} from "@angular/core";
import {MainComponent} from "./administrator/components/main/main.component";
import {LoginComponent} from "./administrator/components/account/login/login.component";
import {WorkorderComponent} from "./administrator/components/entities/workorder/workorder.component";
import {WorkorderViewEditComponent} from "./administrator/components/entities/workorder/workorder.view.edit.component";



const appRoutes: Routes = [

//admin paths
{ path: 'admin', component: AppComponentAdmin},
{ path: 'admin/login', component: MainComponent },
{ path: 'admin/creds', component: LoginComponent },
{ path: 'admin/workorder', component: WorkorderComponent },
{ path: 'admin/workorder/:id', component: WorkorderViewEditComponent },


// otherwise redirect to admin
{ path: 'admin/**', redirectTo: 'admin', pathMatch: 'full' }
];

export const appRoutingProviders: any[] = [
];

export const routing: ModuleWithProviders = RouterModule.forRoot(appRoutes);

In the module i have imported this.

import {HashLocationStrategy, LocationStrategy} from "@angular/common"

and provided like this in the module

[{provide: LocationStrategy, useClass: HashLocationStrategy}]

So now when i visit https://127.0.0.1:8443/admin

i get this in the address bar https://127.0.0.1:8443/#/

What it should give me is this https://127.0.0.1:8443/admin#/

How can i achieve this?

Upvotes: 0

Views: 731

Answers (1)

LoïcR
LoïcR

Reputation: 5039

To configure your routing with a subdirectory, you have to define your base href according to the physical directory.

For example in your case, it should be <base href="/admin">

Upvotes: 1

Related Questions