1

I have read a couple of tutorials about SPA and client-side routing. But I am not getting it completely. How can I setup the router to work without a web server.

Example: file:///C:/cxr/CXR-WebViews/dist/CXR-WebViews/index.html#/products/vat

The browser will accept this as index.html but should i use router fragments for calling components?

I can't use the parameter useHash:true in the router because the resulting url: ".../index.html/#/products/vat" will not find the file. This would only work if it was a webserver call that can redirect requests to index.html.

app-routing.module.ts

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

import { ProductGroupsComponent } from './products/product-groups/product-groups.component';
import { VatComponent } from './products/vat/vat.component';
import { ProductComponent } from './products/product/product.component';
import { AppComponent } from './app.component';

const routes: Routes = [
  { path: '', component: AppComponent, pathMatch: 'full' },
  { path: 'products', component: AppComponent, children:[
    { path: 'productGroups', component: ProductGroupsComponent },
    { path: 'vat', component: VatComponent},
    { path: ':id', component: ProductComponent }
  ]}
];

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

It must be something stupid that i am doing wrong. I just cant find it. I build with: ng build --prod --base-href ./

  • what do you mean by *I have set the Parameter useHash in the router but that seams to onlz work with Servers.*? what is the error that you're getting? – Akber Iqbal Mar 22 '19 at 04:46
  • i am only getting a warning About unhandled Navigation error in main.js the Paragraph before describes what happens with useHash. – Andreas Owen Mar 22 '19 at 05:09
  • Have you looked at https://stackoverflow.com/questions/40679655/angular2-from-file-protocol – ashish.gd Mar 22 '19 at 05:33
  • yes i have read that one but i dont get any erros, all bundles are loaded. i really thinks it is something with the router. do i have to work with Fragments in the router for "index.html#/products/…" to work? – Andreas Owen Mar 22 '19 at 05:54
  • i clarified the Question a bit, and i dont know what else to do. – Andreas Owen Mar 23 '19 at 12:33

1 Answers1

0

i just had to remove the meta tag base-href in index.html and then it worked