Angular 6:services with providedIn: 'root' returns empty object in component










0















My service located in src/core/services/api.service.ts



import Injectable from '@angular/core';

@Injectable(
providedIn: 'root'
)
export class ApiService

constructor()

test()
console.log('hello from services');




I am trying to invoke this service from a component which in another module.



home.component.ts



import Component, OnInit from '@angular/core';
import ApiService from './core/services/api.service';

@Component(
selector: 'app-home',
templateUrl: './home.component.html',
styleUrls: ['./home.component.css']
)
export class HomeComponent implements OnInit

constructor(private api: ApiService)

ngOnInit()
console.log(this.api);





But I am getting an empty object like this ApiService 










share|improve this question

















  • 1





    if it's logging ApiService , then that's an ApiService instance, exactly as intended. You can find the test method under the object's prototype. Are you 100% sure this is not working or did you just guess it?

    – briosheje
    Nov 14 '18 at 9:40












  • yeah, right! Check this, this stackblitz.com/edit/angular-aizqtq ApiService works fine

    – qiAlex
    Nov 14 '18 at 9:41











  • Your code is looking and working perfectly fine. No change is needed.

    – Sunil Singh
    Nov 14 '18 at 9:43











  • Additionaly, if you don't want to do any sort of call, just do: console.log(this.api instanceof ApiService);, if it logs true, it just works.

    – briosheje
    Nov 14 '18 at 10:14















0















My service located in src/core/services/api.service.ts



import Injectable from '@angular/core';

@Injectable(
providedIn: 'root'
)
export class ApiService

constructor()

test()
console.log('hello from services');




I am trying to invoke this service from a component which in another module.



home.component.ts



import Component, OnInit from '@angular/core';
import ApiService from './core/services/api.service';

@Component(
selector: 'app-home',
templateUrl: './home.component.html',
styleUrls: ['./home.component.css']
)
export class HomeComponent implements OnInit

constructor(private api: ApiService)

ngOnInit()
console.log(this.api);





But I am getting an empty object like this ApiService 










share|improve this question

















  • 1





    if it's logging ApiService , then that's an ApiService instance, exactly as intended. You can find the test method under the object's prototype. Are you 100% sure this is not working or did you just guess it?

    – briosheje
    Nov 14 '18 at 9:40












  • yeah, right! Check this, this stackblitz.com/edit/angular-aizqtq ApiService works fine

    – qiAlex
    Nov 14 '18 at 9:41











  • Your code is looking and working perfectly fine. No change is needed.

    – Sunil Singh
    Nov 14 '18 at 9:43











  • Additionaly, if you don't want to do any sort of call, just do: console.log(this.api instanceof ApiService);, if it logs true, it just works.

    – briosheje
    Nov 14 '18 at 10:14













0












0








0








My service located in src/core/services/api.service.ts



import Injectable from '@angular/core';

@Injectable(
providedIn: 'root'
)
export class ApiService

constructor()

test()
console.log('hello from services');




I am trying to invoke this service from a component which in another module.



home.component.ts



import Component, OnInit from '@angular/core';
import ApiService from './core/services/api.service';

@Component(
selector: 'app-home',
templateUrl: './home.component.html',
styleUrls: ['./home.component.css']
)
export class HomeComponent implements OnInit

constructor(private api: ApiService)

ngOnInit()
console.log(this.api);





But I am getting an empty object like this ApiService 










share|improve this question














My service located in src/core/services/api.service.ts



import Injectable from '@angular/core';

@Injectable(
providedIn: 'root'
)
export class ApiService

constructor()

test()
console.log('hello from services');




I am trying to invoke this service from a component which in another module.



home.component.ts



import Component, OnInit from '@angular/core';
import ApiService from './core/services/api.service';

@Component(
selector: 'app-home',
templateUrl: './home.component.html',
styleUrls: ['./home.component.css']
)
export class HomeComponent implements OnInit

constructor(private api: ApiService)

ngOnInit()
console.log(this.api);





But I am getting an empty object like this ApiService 







javascript angular service dependency-injection






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 14 '18 at 9:31









iam batmaniam batman

2,11532450




2,11532450







  • 1





    if it's logging ApiService , then that's an ApiService instance, exactly as intended. You can find the test method under the object's prototype. Are you 100% sure this is not working or did you just guess it?

    – briosheje
    Nov 14 '18 at 9:40












  • yeah, right! Check this, this stackblitz.com/edit/angular-aizqtq ApiService works fine

    – qiAlex
    Nov 14 '18 at 9:41











  • Your code is looking and working perfectly fine. No change is needed.

    – Sunil Singh
    Nov 14 '18 at 9:43











  • Additionaly, if you don't want to do any sort of call, just do: console.log(this.api instanceof ApiService);, if it logs true, it just works.

    – briosheje
    Nov 14 '18 at 10:14












  • 1





    if it's logging ApiService , then that's an ApiService instance, exactly as intended. You can find the test method under the object's prototype. Are you 100% sure this is not working or did you just guess it?

    – briosheje
    Nov 14 '18 at 9:40












  • yeah, right! Check this, this stackblitz.com/edit/angular-aizqtq ApiService works fine

    – qiAlex
    Nov 14 '18 at 9:41











  • Your code is looking and working perfectly fine. No change is needed.

    – Sunil Singh
    Nov 14 '18 at 9:43











  • Additionaly, if you don't want to do any sort of call, just do: console.log(this.api instanceof ApiService);, if it logs true, it just works.

    – briosheje
    Nov 14 '18 at 10:14







1




1





if it's logging ApiService , then that's an ApiService instance, exactly as intended. You can find the test method under the object's prototype. Are you 100% sure this is not working or did you just guess it?

– briosheje
Nov 14 '18 at 9:40






if it's logging ApiService , then that's an ApiService instance, exactly as intended. You can find the test method under the object's prototype. Are you 100% sure this is not working or did you just guess it?

– briosheje
Nov 14 '18 at 9:40














yeah, right! Check this, this stackblitz.com/edit/angular-aizqtq ApiService works fine

– qiAlex
Nov 14 '18 at 9:41





yeah, right! Check this, this stackblitz.com/edit/angular-aizqtq ApiService works fine

– qiAlex
Nov 14 '18 at 9:41













Your code is looking and working perfectly fine. No change is needed.

– Sunil Singh
Nov 14 '18 at 9:43





Your code is looking and working perfectly fine. No change is needed.

– Sunil Singh
Nov 14 '18 at 9:43













Additionaly, if you don't want to do any sort of call, just do: console.log(this.api instanceof ApiService);, if it logs true, it just works.

– briosheje
Nov 14 '18 at 10:14





Additionaly, if you don't want to do any sort of call, just do: console.log(this.api instanceof ApiService);, if it logs true, it just works.

– briosheje
Nov 14 '18 at 10:14












1 Answer
1






active

oldest

votes


















1














So it is returning an object and that means that the service is initialized correctly, instead of console.log(this.api); try this.api.test(), you should see hello from services in the console.






share|improve this answer






















    Your Answer






    StackExchange.ifUsing("editor", function ()
    StackExchange.using("externalEditor", function ()
    StackExchange.using("snippets", function ()
    StackExchange.snippets.init();
    );
    );
    , "code-snippets");

    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "1"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader:
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    ,
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );













    draft saved

    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53296914%2fangular-6services-with-providedin-root-returns-empty-object-in-component%23new-answer', 'question_page');

    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    1














    So it is returning an object and that means that the service is initialized correctly, instead of console.log(this.api); try this.api.test(), you should see hello from services in the console.






    share|improve this answer



























      1














      So it is returning an object and that means that the service is initialized correctly, instead of console.log(this.api); try this.api.test(), you should see hello from services in the console.






      share|improve this answer

























        1












        1








        1







        So it is returning an object and that means that the service is initialized correctly, instead of console.log(this.api); try this.api.test(), you should see hello from services in the console.






        share|improve this answer













        So it is returning an object and that means that the service is initialized correctly, instead of console.log(this.api); try this.api.test(), you should see hello from services in the console.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 14 '18 at 9:41









        Mac_WMac_W

        845515




        845515



























            draft saved

            draft discarded
















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid


            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.

            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53296914%2fangular-6services-with-providedin-root-returns-empty-object-in-component%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Top Tejano songwriter Luis Silva dead of heart attack at 64

            政党

            天津地下鉄3号線