I'm creating a custom library in Angular 7 and I would like to somehow export a function in this library that can be used in the main application after the library is installed. I saw some documentation about doing this through a typings.d.ts file, but after trying it out, it seems the typings file can only be installed through npm, which doesn't work for me since this library will not be uploaded to npm. Is there a way to export a function without having to upload the library to npm?
-
You mean, you want to use it as local npm module?andsilver– andsilver2020-02-24 03:15:38 +00:00Commented Feb 24, 2020 at 3:15
-
I am also using an angular project(repo1) as a dependency in another angular project(repo2). I want to export some common functions from repo1 that can be used in repo2. This is compiling successfully but giving an error on the browser console. Uncaught TypeError: (0 , som_core__WEBPACK_IMPORTED_MODULE_0__.functionName) is not a function. Did you find any solution ?Akshay Kapoor– Akshay Kapoor2021-12-05 05:54:19 +00:00Commented Dec 5, 2021 at 5:54
3 Answers
I ended up doing a different approach. I set up my custom library to take IN a function to be executed after a save function was completed, instead of exporting the library's function. I set it up just like how the chosen answer was done here: Angular 5 Component input function that takes parameters
Comments
After some digging I was able to figure this out...
Add this line to your public-api.ts file in your custom library. (No file extension needed here, just like the other lines you'll have in there; And it should auto-complete as you type it out.)
export * from './lib/some-path/file-name-that-has-functions';
Then import your function in the application that's using the library.
import {myFunctionName} from "my-custom-library";
You would do the same thing if you had an enum, a variable or some other exported member you wanted to use in the client application but keep in a custom library.
It's been a few years, but if I was looking for it someone else probably is too. Just remember to ng build my-custom-library after making this change!