custom created method error: “is not a function”

后端 未结 2 1897
被撕碎了的回忆
被撕碎了的回忆 2021-01-13 11:23

I have a list of hero-buttons with a custom animation created in button.component.ts. At the beginning, they are inactive. When I press one of then, the selecte

相关标签:
2条回答
  • 2021-01-13 11:34

    Your service most probably only returns plain objects, meaning they don't have the member methods of the Hero class.

    You need to explicitly create new Hero() objects to have the toggleState() method available on the hero object.

    0 讨论(0)
  • 2021-01-13 11:50

    If you cast JSON to a TypeScript class, all is happening is that you indicate to the static analysis it can safely assume the value is of that class; that doesn't actually change the JSON value at all (i.e it doesn't make it an instance of that class).

    What you want is probably How do I cast a JSON object to a typescript class or Cast JSON object to TypeScript class instance

    0 讨论(0)
提交回复
热议问题