I have my main component (cmp1) and another 3 other components (helpers/services, they dont have html, only js).
In cmp1 I'm calling helper1 that calls helper2
cmp1->helper1->helper2
helper1 throws error
helper2.initializeContext is not a function
Im importing with import {initializeContext} from 'c/helper2'
notation and helper2 have lwc class structure but helper1 dont (its similar to mortgage.js, only methods with export).
I've tried to import with but still not working
import initializeContext from '../helper2/helper2.js'
Helper2
import { LightningElement, api, wire, track } from "lwc";
export default class sheetHelper extends LightningElement {
@api context;`
initializeContext(parentsContext) {
this.context = parentsContext;
}
Helper1
import { initializecontext } from 'c/helper2';
function initialize(parentsContext) {
initializeContext(parentsContext); <-throw error
...
}
...
export { initialize }
Main component
import { initialize } from 'c/helper1';
export default class PedidosExcelDealer extends LightningElement {
renderedCallback() {
initialize(this.template);
}
}
Best Answer
To define and import a value or function the code to import would look e.g. like this:
and you could import it and use it e.g. like this:
In your code, the
initializeContext
method is a class method i.e. only makes sense via an instance of aLightningElement
.You could use inheritance, but note this from the Salesforce documentation: