11

I have Apex Controller Class, say, LWCController and several methods inside of it, let's say Method1, Method2, Method3, Method4, Method5.

Documentation says that to

Use default import syntax to import an Apex method via the @salesforce/apex scoped packages.

enter image description here

Default import syntax allows to imports several items from the same module

import { export1 , export2 } from "module-name"; 
import { foo , bar } from "module-name/path/to/specific/un-exported/file"; 
import { export1, export2 as alias2 , [...] } from "module-name"; 
import defaultExport, { export [ , [...] ] } from "module-name";

enter image description here

I am trying to import all of them simultaneously at the same line

import {Method1, Method2, Method3, Method4, Method5 } from '@salesforce/apex/LWCController';

And I receive the following errors

=== Deploy Errors PROJECT PATH  ERRORS    
N/A           [Line: 2, Col: 118] LWC1501: @salesforce/apex modules should have both class and method names.
N/A           LWC1513: @salesforce/apex modules only support default imports.
2
  • 1
    import {Method1, Method2, Method3, Method4, Method5 } from '@salesforce/apex/LWCController'; how will SF know which alias to use for which method? WHat if there are 10 methods in class and you importing 5 ? Mar 19 '19 at 9:57
  • 1
    @PranayJaiswal Default aliases could be used, Method1 for Method1 etc. If I import 5 methods, I want these 5 methods be available with default aliases, for the other 5 methods I don't care.
    – Patlatus
    Mar 19 '19 at 10:03
8

Looks like here by "default" imports meant only imports which import one item from the module, so I have to use the following workaround.

import Method1 from '@salesforce/apex/LWCController.Method1';
import Method2 from '@salesforce/apex/LWCController.Method2';
import Method3 from '@salesforce/apex/LWCController.Method3';
import Method4 from '@salesforce/apex/LWCController.Method4';
import Method5 from '@salesforce/apex/LWCController.Method5';

which is nasty but the only available workaround.

4
  • 7
    It's not a workaround, it is intended behavior. The reason why is because the methods can be bound by metadata, like they are in Visualforce, so you can't accidentally delete them from the class.
    – sfdcfox
    Mar 18 '19 at 19:32
  • 4
    If I was able to import several methods at once, they would be still bound by metadata, but my code would look much more elegant and clean
    – Patlatus
    Mar 18 '19 at 20:08
  • 1
    I know what you're saying, but that's not how it was engineered. They also didn't give us the wildcard import syntax, either. Maybe we'll get it in a future release? At any rate, we're stuck with what we have.
    – sfdcfox
    Mar 18 '19 at 20:11
  • 3
    I agree that we are stuck with what we have.
    – Patlatus
    Mar 18 '19 at 20:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.