我注意到LWC中有一些有趣的行为,这些行为正在建立,并且无法找到有关该原因的太多信息。基本上,我有一个带有多个签名的Apex方法:
// myMethod with 2 param signature
@AuraEnabled
public static void myMethod(String param1, String param2) {
// I expect this method to be invoked when called from the LWC code shown below
....
}
// myMethod with 3 param signature
@AuraEnabled
public static void myMethod(String param1, String param2, Boolean param3) {
// However this method gets invoked instead
....
}
当我尝试从LWC调用 myMethod
时,只将两个参数传递到该方法中时,我希望将调用2个参数签名方法但是,发生的是调用3个参数签名方法,并且 null
的值作为第三个参数值传递。
runJob({param1, param2}).then(value => ... )
这是预期的行为吗?当我通过APEX调用方法时,正确的方法会被其签名调用,但是从LWC调用该方法时,情况并非如此。
我有没有办法从LWC调用正确签名的 myMethod
APEX方法?
I've noticed some interesting behavior in an LWC that I am building and haven't been able to find much info on the cause. Basically I have an Apex method declared with multiple signatures:
// myMethod with 2 param signature
@AuraEnabled
public static void myMethod(String param1, String param2) {
// I expect this method to be invoked when called from the LWC code shown below
....
}
// myMethod with 3 param signature
@AuraEnabled
public static void myMethod(String param1, String param2, Boolean param3) {
// However this method gets invoked instead
....
}
When I attempt to call myMethod
from an LWC, and only pass two parameters into the method, I would expect that the 2 param signature method would be invoked, however what happens is that the 3 param signature method is invoked, and the value of null
is passed as the third params value.
runJob({param1, param2}).then(value => ... )
Is this expected behavior? When I call the methods through apex, the correct method is invoked by its signature, however this doesn't seem to be the case when calling the method from an LWC.
Is there a way for me to invoke the myMethod
Apex method of the correct signature from an LWC?
发布评论
评论(1)
编辑:
将在22夏季发行的编译时间禁止使用。
原始:
它比您想象的要差。
runjob({param2,param1})
也可以正常工作。 “正确”的意思是他们的名字很重要,而不是位置!您的内容总是作为对象传递,而不是参数列表。您可以拥有一个辅助Apex包装器类,并且(假设所有字段均为
@auraenabled
),它将正确映射它们。如果您有参数列表 - 好吧,即使在调用代码之前,也会发生一种拆箱和类型匹配。如果您将“ ABC”传递给日期变量 - 即使在代码运行之前就会丢弃JSON避难所错误,则您无需捕获它。所以...选择不同的名字?或将它们汇合,以便3-Param版本查看最后一个参数,并在需要时调用2-Param One,并且业务逻辑允许。通常 - 保持简单,留下一些评论和良好的单位测试,或者从现在起2个月后,贫穷的维护人员会挣扎。
(如果使用 apex pmd pmd pmd plagin href =“ https://forcedotcom.github.io/sfdx-scanner/” rel =“ nofollow noreferrer”> sfdx scanner )具有长参数列表的功能,无论如何: https://pmd.github.ioio
of
Edit:
it will be banned at compile time in Summer'22 release. https://help.salesforce.com/s/articleView?id=release-notes.rn_apex_ValidationForAuraEnabledAnnotation.htm&type=5&release=238
Original:
It's worse than you think.
runJob({param2, param1})
will work correctly too. "Correctly" meaning their names matter, not the position!Your stuff is always passed as an object, not a list of parameters. You could have a helper Apex wrapper class and (assuming all fields are
@AuraEnabled
) it'll map them correctly. If you have list of parameters - well, a kind of unboxing and type matching happens even before your code is called. If you passed "abc" to a Date variable - a JSON deserialization error is thrown even before your code runs, you have no means to catch it.https://developer.salesforce.com/docs/component-library/documentation/en/lwc/lwc.apex_wire_method
So... pick different names? Or funnel them so the 3-param version looks at the last param and calls 2-param one if needed and the business logic allows it. Generally - keep it simple, leave some comments and good unit tests or 2 months from now poor maintenance guy will struggle.
And (if you use the Apex PMD plugin and/or sfdx scanner) functions with long parameter lists are frowned upon anyway: https://pmd.github.io/latest/pmd_rules_apex_design.html#excessiveparameterlist
See also