I am trying to call an Apex method imperatively from LWC. The method accepts an Integer parameter. When I call this Apex method using a wired method it works fine, without any error. But when I call it imperatively from the LWC, it throws an error Value provided is invalid for action parameter 'i' of type 'Integer'
import { LightningElement, track, wire } from 'lwc';
import method from '@salesforce/apex/MyClass.method';
export default class MyComponent extends LightningElement {
i = 5;
@track messageFromImperative = '';
@track messageFromWiredCall = '';
@track errorFromImperative = '';
@track errorFromWiredCall = '';
@wire(method, {i: '$i'})
getMessage({data, error}) {
this.messageFromWiredCall = data;
this.errorFromWiredCall = error;
}
handleClick() {
method({i: '$i'})
.then(result => { this.messageFromImperative = result })
.catch(error => { this.errorFromImperative = error })
}
}
//Apex class
public with sharing class MyClass {
@AuraEnabled(cacheable=true)
public static string method(Integer i){
return 'Integer sent in '+ i;
}
}