0

How to make it so that in Angular, you can use the plus sign in the path.

For example: http://localhost:4200/how+to+do

Instead, angular changes plus to %2B

For example: http://localhost:4200/how%2Bto%2Bdo

How to fix it?

Maks
  • 11
  • 4
  • Does this answer your question? [Why do you need to encode URLs?](https://stackoverflow.com/questions/2152738/why-do-you-need-to-encode-urls) – Yong Shun Mar 25 '23 at 09:33
  • there is a [way](https://stackoverflow.com/questions/41476193/angular-2-disable-url-encoding), but do you really need it? – Andrei Mar 25 '23 at 09:35
  • @Andrei - Yes, I need him. I think I found one way, but I will not refuse your option – Maks Mar 25 '23 at 09:41
  • You can use `+` in your urls but its not recommended for SEO nor clients (because of the way it displays in share links) BUT you can use `+` and access the value correctly by using `encodeURIComponent` and `decodeURIComponent`. I hope it helps. – Farhad Mar 25 '23 at 14:57

1 Answers1

0

Angular's default routing configuration automatically converts certain characters, such as plus signs, in URLs. To prevent this automatic conversion, you can modify your application's routing configuration.

To disable the automatic conversion of plus signs, you can add the useHash parameter to the forRoot method in the routing module and set its value to true. This will enable "routing hash" in the app, meaning the URL will have a # sign in front of the routing path. Using hash routing ensures that plus signs are not automatically converted to %2B.

@NgModule({
  imports: [RouterModule.forRoot(routes, { useHash: true })],
  exports: [RouterModule]
})
SparrowVic
  • 274
  • 1
  • 7