typescript - ionic3/angular 4 : Injecting a service in another service not working -


i trying make simple use of service within service (not component) dependency injection in ionic3/angular4. thought had understood how in theory, in practise have been struglying several days...

here configuration :

cli packages:     @ionic/cli-plugin-ionic-angular : 1.4.1     @ionic/cli-utils                : 1.7.0     ionic (ionic cli)               : 3.7.0 local packages:     @ionic/app-scripts : 2.1.3     ionic framework    : ionic-angular 3.6.0 

and tsconfig.json file (as have gone throught threads suggesting compiling options have effects on injection behavior)

{   "compileroptions": {     "allowsyntheticdefaultimports": true,     "declaration": false,     "emitdecoratormetadata": true,     "experimentaldecorators": true,     "lib": [       "dom",       "es2015"     ],     "module": "es2015",     "moduleresolution": "node",     "sourcemap": true,     "target": "es5"   },   "include": [     "src/**/*.ts"   ],   "exclude": [     "node_modules"   ],   "compileonsave": false,   "atom": {     "rewritetsconfig": false   } } 

in app.module.ts, have registered services importing associated modules : ngloggermodule & ionicstoragemodule, others adding them providers array : settingsstorageprovider & tmdbapiprovider :

@ngmodule({ .... imports: [     ionicstoragemodule.forroot(),     ngloggermodule.forroot(level.log) ], providers: [     ....     settingsstorageprovider,     tmdbapiprovider ] 

this settingsstorageprovider class can make use of storage & logger services without problem :

@injectable() export class settingsstorageprovider {      constructor(public storage: storage, public logger: logger) {         this.logger.log('hello settingsstorageprovider');     }      public savesettings(_settings: settingsmodel){     .... }      public loadsettings(): promise<settingsmodel> {     ....} } 

this tmdbapiprovider class cannot make use of injected services logger or settingsstorageprovider thought seem correctly registered in app.module.ts :

@injectable() export class tmdbapiprovider {  static parameters() {     return [[http]]; }     constructor(public settingsstorage: settingsstorageprovider, public http: http,      public logger: logger) {      if (settingsstorage == null)          console.log('tmdbapiprovider:constructor-->settingsstorage null !!!');      if (logger  == null)          console.log('tmdbapiprovider:constructor-->logger null !!!');      if (http == null)          console.log('tmdbapiprovider:constructor-->http null !!!'); }      searchmovies(moviename) {         this.settingsstorage.loadsettings().then((_settings) => {         .....             var url = config.data.tmdb_searchservice + encodeuri(moviename) + '&'+config.data.tmdb_apikey+'&'+config.data.tmdb_language+_usersettings.getlanguage();             var response = this.http.get(url).map(res => res.json());              return response;         });     } } 

i have no errors @ compile time, @ runtime, got following debug logs have put in constructor :

tmdb-api.ts:30 tmdbapiprovider:constructor-->logger null !!!

tmdb-api.ts:34 tmdbapiprovider:constructor-->http null !!!

and following error :

error error: uncaught (in promise): typeerror: _this.settingsstorage.loadsettings not function typeerror: _this.settingsstorage.loadsettings not function     @ tmdb-api.ts:48     @ new t (polyfills.js:3)     @ tmdbapiprovider.webpackjsonp.171.tmdbapiprovider.searchmovies 

if invert order of injected services in constructor :

@injectable() export class tmdbapiprovider {      constructor(public http: http, public logger: logger,     public settingsstorage: settingsstorageprovider) { 

i got different debug logs have put in constructor :

tmdbapiprovider:constructor-->settingsstorage null !!! tmdb-api.ts:31 tmdbapiprovider:constructor-->logger null !!!

and error has changed :

error error: uncaught (in promise): typeerror: cannot read property 'loadsettings' of undefined typeerror: cannot read property 'loadsettings' of undefined 

it seems first service called tmdbapiprovider constructor injected.

anyone has ever faced strange behavior ? thank in advance provide me.

[15/08/2017] here complementary information problem:

the root "ngmodule" "declarations", "imports", "entrycomponents" & "providers" of myapp : ngmodule , view of injector graph of myapp using "augury" chrome extension : injector graph

strangely, "tmdbapiprovider" declared provider in root "ngmodule" not part of injector graph... know why ? thank in advance (it's driving me nuts !).

enter image description here

yes, must remove static keyword on parameters() method if need use service through di.that design.but if need use static methods don't need instantiate class through di.you can use below when need on component:

import { tmdbapiprovider } './tmdbapiprovider';  export class yourcomponent {      constructor() {         // example of accessing static members of class.         // note: didn't inject service, nor manually instantiate          let value = tmdbapiprovider.parameters();      }   } 

Comments

Popular posts from this blog

android - InAppBilling registering BroadcastReceiver in AndroidManifest -

python Tkinter Capturing keyboard events save as one single string -

sql server - Why does Linq-to-SQL add unnecessary COUNT()? -