I have three LWC's called (not real names) : mainLwc
, firstLwc
and secondLwc
.
The mainLwc
has helper JS file called mainLwcHelper.js
and it has one function inside it.
The problem is that I can import this function in firstLwc
, but can't in secondLwc
or any other lwc:
Here all code all of my LWC:
mainLwc.html The LWC structure
<template>
<template if:true={showFirst}>
<c-first-lwc></c-first-lwc>
</template>
<template if:true={showSecond}>
<c-second-lwc></c-second-lwc>
</template>
</template>
mainLwcHelper.js
const doSomething = ( recordsJSON ) => {
try {
let records = JSON.parse(recordsJSON);
// some logic, not important !!
return records;
} catch (error) {
console.error(error);
}
};
export{ doSomething};
mainLwc.js
import { LightningElement } from 'lwc';
export { doSomething } from './mainLwcHelper';
export default class MainLwc extends LightningElement {
showFirst = true;
showSecond = false;
// code to switch between LWC's
}
firstLwc.js import function and try to call it
import { LightningElement } from 'lwc';
import { doSomething } from 'c/mainLwc';
export default class FirstLwc extends LightningElement {
connectedCallback(){
try {
console.log( doSomething ); // getting function code here
console.log( typeof doSomething );//return `function`
let list = doSomething( '[]'); // executes without error !!
} catch (error) {
console.error(error);
}
}
}
secondLwc.js the same code with first, but getting error here !!
import { LightningElement } from 'lwc';
import { doSomething } from 'c/mainLwc';
export default class SecondLwc extends LightningElement {
connectedCallback(){
try {
console.log( doSomething ); // getting `undefined` here
console.log( typeof doSomething ); // returns `undefined`
let list = doSomething( '[]'); // getting error here !!
} catch (error) {
console.error(error);
}
}
}
I did try:
- renaming functions
- I checked this one, however it does not help Importing Function from LWC js File returns 'Is Not Function'
- everything from here Share JavaScript Code
- calling the function from other LWC's, only the first LWC works
- as possible solution is to create JS helper in each LWC, however I want to find out why it is not working
Have anyone already faced something similar ?
Any help will be appreciated. Thanks You.