DotNetDev
DotNetDev

Reputation: 25

Angular 2 Routing - Open Links from address bar

When I try to open Angular 2 routes from the address bar I continually receive http 404 responses, however navigating to the routes using anchors with "routerLink" directives works without issue. I've attempted to modify my web.config file for url rewrites as instructed in Angular's Deployment Guide but this usually breaks my routing altogether. Lastly, I did include <base href="/"> in my index.html file, but this also has not helped. Any assistance from the community is much appreciated!

Here's my routing component code:

import { NgModule } from '@angular/core';
import { RouterModule, Routes } from '@angular/router';
import { ClientsComponent } from './clients.component';

const routes: Routes = [

    { path: 'clients', component: ClientsComponent },
     //^This does not work from address bar, only from routerLinks.
    {
        path: '**',
        redirectTo: '',
        pathMatch: 'full'
    },   
];

@NgModule({
    imports: [RouterModule.forRoot(routes)],
    exports: [RouterModule]
})

export class AppRoutingModule { }

Upvotes: 2

Views: 1172

Answers (1)

Mani S
Mani S

Reputation: 2561

Try setting usehash to true, this should solve your problem.

RouterModule.forRoot(routes, { useHash: true })

Upvotes: 2

Related Questions