function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
LaurentDelcambreLaurentDelcambre 

LWC: how to export/import module functions?

I am trying to export some Javascript code and import it in another module.
Following the documentation I did this:
mortgage.js:
const getTxt = () => {
    console.log('yo getTxt');
    return 'yoyoyo';
}
export {getTxt};
in another component: (it's NOT the full code, it's just to show that I don't use this.getTxt(). I call it directly like the example in the documentation
import getTxt from 'c/mortgage';
export default class another extends LightningElement {
    this.sectionDescription = getTxt();
}

The code stops working at the line I call getTxt();
No error in the console.
My console.log code is not even called.

Any suggestion?


 
Raj VakatiRaj Vakati
Try like this
 
export function getTxt() {
    console.log('yo getTxt');
    return 'yoyoyo'
}

This is working for me
 
/* utils.js */
/**
 * Returns whether provided value is a function
 * @param {*} value - the value to be checked
 * @return {boolean} true if the value is a function, false otherwise
 */
export function isFunction(value) {
    return typeof value === 'function';
}
 
import { LightningElement ,api,track} from 'lwc';
// import the library
import {
    isFunction
} from 'c/utils';

export default class Libcaller extends LightningElement {
@track result;
checkType() {
    // call the imported library function
    this.result = isFunction(
        function () {
            console.log(" I am a function");
        }
    );
}
}