How to utilise URLSearchParams in Angular 2?

前端 未结 2 1339
无人及你
无人及你 2021-01-07 23:22

I\'ve been hours now trying to figure out how to correctly inject the URLSearchParams dependency into a component.

In my boot.ts I\'m makin

2条回答
  •  北恋
    北恋 (楼主)
    2021-01-07 23:50

    So after a lot of research and help from Angular's Gitter, I've found out the answer to two of my problems.

    Firstly, URLSearchParams cannot be injected in the constructor as a dependency like I'm trying to do - it can be newed however.

    var params = new URLSearchParams()

    It also cannot be used as I was hoping it could be, which was to update the location. It appears to be simply a utility function for parsing query strings and setting query params which you can then use with Router's navigate method.

    Secondly, in order to access RouteParams it appears that your component needs to be instantiated by the Router, i.e. associating your component with a path in a @RouteConfig decorator.

    This was not my case as I had a in the shell of my application which was not instantiated by the Router.

    This is my current understanding and hopefully this helps others with the same problem.

提交回复
热议问题