How to execute a function from another component that is NOT a sibling of the first component?

后端 未结 4 1599
长情又很酷
长情又很酷 2020-12-04 00:06

I\'m trying to execute a function from another component (These 2 components are NOT siblings). My guess is that I will need to use @Output and even

4条回答
  •  余生分开走
    2020-12-04 00:44

    Depending on what you are trying to do, you have two options.

    First: If you need Angular features (e.g. dependency injection) or want to share data, you should think about using a service. There is enough of documentation and tutorials already (see angular docs.) I recommend reading this thoroughly.

    Second: In case you just need JavaScript capabilities, you could create a TypeScript file (e.g. /src/app/shared/tools.ts and put your method there. This is mostly useful for static functions that do some kind of calculations, formatting or similar. You can then import and use the function wherever you need it. (Tip: make it pure.)

提交回复
热议问题