How to expose methods from NativeScript custom components


Basically, the question is asked perfectly here:

Just that the only answer there doesn’t really answer the question and offers a solution only for decoupled functionality.

I was hoping here would be a good place to find out how to properly expose methods from components.

Thank you!


So you’re wanting to call a method that is within another class? Is that what you’re wanting?

And are you using vanilla nativescript or nativescript with angular?


I was looking for the best (recommended ?) way for my custom components to expose methods to the controllers that need them, other that putting the methods on the root element of the component, as mentioned in the StackOverflow question or making a separate module as in the answer to the question.

As the question mentions, exporting the methods from the module doesn’t add them to the element prototype.

I don’t necessarily mind placing the methods on the root element of my custom component, but I have the same opinion as the question author that it looks kind of hackish and probably there as more “nativescript-way”.

@mast3rd3mon I’m using vanilla JS


Do you mean so you can use the method in the custom component tag in another page?

Ah, vanilla js, not too sure i can help here then. I typically use angular


Can you please take a look at the question on StackOverflow, it gives a perfect example of the thing I want to achieve.


I did. And seen as though the only solution i know is an angular solution, I’m not going to be of any help to you


You might want to answer the question on SO anyway, maybe others will find your Angular solution useful. Also, you made me curious :slight_smile:


Same problem here, is there any solution @surdu?


@clifte Nope. Just exposed some methods from the module, import the module in the controller and use the methods …


But come to think about it, this compromises the reusability nature of the component. I need different components to have their own methods that reflect and affect their own properties.

I’ve found this two new {N} docs pages that seems to be what we need: