reyesoft / ngx-jsonapi

JSON API client library for Angular 5+ 👌 :: Production Ready 🚀
https://ngx-jsonapi.reyesoft.com/
MIT License
101 stars 52 forks source link

Work with angular 8 #205

Closed yringler closed 4 years ago

yringler commented 5 years ago

Replicated at https://github.com/yringler/ngx-issue-replicate

ng test fails with

TypeError: Class constructor BookService cannot be invoked without 'new'

Changing typescript target to es5, as per related issues below, fixes this. That should not be needed; it causes bigger builds, and means that ngx-jsonapi doesn't work out of the box with angular.

After changing to es5, I get error

TypeError: Cannot read property 'registerService' of undefined

Also, the README reccomends using @Injectable() unstead of

@Injectable({
  providedIn: 'root'
})

Doing so causes error in test

NullInjectorError: StaticInjectorError[newCtor]: NullInjectorError: No provider for newCtor!

Related Issues have a similar error, and tell to change typescript target to es5.:

  1. https://github.com/valor-software/ngx-bootstrap/issues/3134
  2. https://github.com/lukasz-galka/ngx-gallery/issues/242#issuecomment-483223817

https://github.com/bugsnag/bugsnag-js/issues/505 is possibly relevant, also.

Replicate

  1. ng new ngx-issue-replicate
  2. yarn add ngx-jsonapi@2.0.0-rc.4 --save (as per readme. I had the same issue without specifying version)
  3. ng g service book
  4. Write simple service
  5. run test
pablorsk commented 4 years ago

Any ideas @maxi7587 ? You check v2.1x work with angular 8?

scandinave commented 4 years ago

Same here. Removing the autoregister() annotation with :

@Injectable({
   providedIn: 'root' 
})

make things work.

Tested with 2.1 and Angular 8.x

maxi7587 commented 4 years ago

You can use providedIn to provide the service in the app root instead of placing it in your NgModule, but you'll still have to register it using @Autoregister() or .register() (the second one in the service's constructor. This is because the library creates a map using the service's type when you register it. This map is used when calling getService() from a resource, for example, when receiving a resource from the back-end.