Recurrent functions

#1

Hi,

I’m using in my project some custom functions that I need to copy and paste on every pages, I think it’s useless and not a clean way to do. Does anyone knows a solution that I can use to store all my “custom” functions? Like a class to import in all files or something like that :slight_smile:

Thank you,
Sreadon

#2

very simple make a ts file in some where and put your common functions there…from a component or service where ever you need to use it use it like import {FunctionName} from "./utils.ts";

#3

Hi,

Sorry for the late response, didn’t took time to look into this.
So, I’ve tried what you said but didn’t managed to get it working.

As an example, I’ve a getFirstLetter function, which, as the name says, return the first letter of a string.
I’ve created a .ts file which contains

export getFirstLetter(string) {
// blablabla
}

And in my other TS file, I’ve imported it, my ide is not reporting any TS errors but when I’m running the live test, it’s not working, telling me it can’t find the function.

Any ideas please?

Thanks,
Sreadon

#4

You have to provide some more info. How is it not working? How do you define the function and how are you calling it?
Code snippets always helps people understand your problem a bit better.

#5

Don’t know what to say more :confused:

I have my home.ts file which calls the function getFirstLetter();
So, at the beginning of this file, I’ve put

import {getFirstLetter} from "../utils/utils.ts";

And to call it, I’m simply doing: getFirstLetter(var);

When I load the web page, I have an error in Chrome’s console telling me that it can’t find the function that I’m calling (getFirstLetter).

In the utils.ts file, I’ve ONLY:
export getFirstLetter(string) { // blablabla }

Should I put something else in it?

1 Like
#6

Maybe you already know the answer by now, but for anyone that got here trying to achieve the same result, the only thing missing in your code was specify that you’re exporting a function, like this:
export function getFirstLetter(string) { // blablabla }