0

I have the following function:

export function toCamelCase(string): string {
  //code omitted
}

Then I use it on my feature components by adding reference as:

import * as utility from '../shared/functions/helper-functions';

However, I use a shared module and I am wondering if it is possible to add this helper function reference only to shared module and export it so that I can use its functions without adding its reference (I already add shared.module reference). Any idea?

import * as utility from '../shared/functions/helper-functions';

@NgModule({
  declarations: [
    //...
  ],
  imports: [
    //...
  ],
  exports:  [
    // I want to add helper function as a module    
  ],
  providers: [
    //...
})
export class SharedModule { }
Bergi
  • 630,263
  • 148
  • 957
  • 1,375
Jack
  • 1
  • 21
  • 118
  • 236

1 Answers1

0

Just export class SharedModule and export * from '../shared/functions/helper-functions' as well.

If you really want to expose the helper functions in the Angular module, you could expose them as services (in the providers section) but that wouldn't really make sense.

Guerric P
  • 30,447
  • 6
  • 48
  • 86
  • Thanks, but my `export function toCamelCase(string) {}` function is not inside a class, that is the whole definition in that file. So, could you pls post is as inside a class definition? And should it (`helper-functions`) be a class, component or module – Jack Jan 01 '21 at 21:30
  • I am referring: https://medium.com/@OlegVaraksin/what-is-the-best-way-to-write-utilities-in-typescript-e3cae916fe30 – Jack Jan 01 '21 at 21:38
  • @jahnsen as the article states, the best solution is just to `export function` that are not members of a class so the `helper-functions` should just contain exported functions and that makes it an ECMAScript module – Guerric P Jan 02 '21 at 21:36