Angular error when returning function string in service: Type '() => string' is not Assignable to type 'string'

Asked

Viewed 351 times

0

I’m having the following problem while trying to return a string of a variable of service for any other function, which is strange because all variables are strings like the return of functions.

test.service.ts

private oneString:string = "hi";

getOneString(): string {
    return this.oneString.
}    

myFunction.ts

constructor(private testeService: TesteService) { } 

private myString:string = this.service.getOneString; 

console.log(myString);

Error: Type '() => string' is not Assignable to type 'string'

  • 1

    Did you notice that in your constructor you declared testeService and in myString called service?

  • 1

    Oops, typo, but thank you.

  • Leave the question uncorrected so the next person who searches for this error can also be helped.

  • 1

    beauty, already returned to the service.

2 answers

3

In the builder you call your service TesteService with the following name testeService, but to call the service you put in the code this.service, that is, to work you must put this.testeService, as below

myFunction.ts

constructor(private testeService: TesteService) { } 

private myString:string = this.testeService.getOneString(); 

console.log(myString);

0


The problem was I was calling this.testeService.getOneString, and should be called was: this.testeService.getOneString() Also as commented the other problem was this.service, which should be this.testeService.

Solved !

Browser other questions tagged

You are not signed in. Login or sign up in order to post.