Angular2 without hash in url

Now my site URL looks like this because I use the one described below

http: // localhost: 4200 / # / cadastro

Is it possible to remove the hash in the url and not get the 404 error?

EDIT: Router Module Added

const appRoutes: Routes = [ { path: '', component: HomeComponent }, { path: 'cadastro', component: CadastroNoivosComponent }, { path: '**', component: HomeComponent } ]; export const routing = RouterModule.forRoot(appRoutes); 
+6
source share
2 answers

If you use PathLocationStrategy as described here , you can remove the hash in the URL.

But getting rid of the 404 error requires some configuration on the server side. A quick and easy way is to configure the server to load the home page when requesting any URL of the form http://yourhost/* .

+6
source

If you use the Angular finale, the reasons for the hash may be:

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

Thus, removal can help.

 RouterModule.forRoot(yourRoutesHere) 

Alternatively, if you used in your providers (in NgModule):

 {provide: LocationStrategy, useClass: HashLocationStrategy} 

just delete it.

EDIT, if you need a LocationStrategy, try changing HashLocationStrategy to PathLocationStrategy :

 {provide: LocationStrategy, useClass: PathLocationStrategy} 

Learn more about LocationStrategy here.

Now that I have seen your routes and regarding your 404 problem, you can try changing the following

 { path: '**', component: HomeComponent } 

in

 { path: '**', redirectTo: '', pathMatch: 'full' } 

More about routing here.

Also check that in index.html you set basehref as follows:

 <base href="/"> 
+22
source

Source: https://habr.com/ru/post/1014107/


All Articles