【发布时间】:2021-01-13 02:20:21
【问题描述】:
在 blazor 项目中,JavaScript 函数区分 null 和 undefined。当从 C# (await jsRuntime.InvokeAsync<long>("func", null, "str")) 传递 null 时,JS 端接收值 null (typeof arg1 === 'object')。从 C# 端传递什么来接收undefined (typeof arg1 === 'undefined')?
编辑:
显然null 的行为并不一致,这取决于参数的位置。
JS:
func1: function (arg1) {
console.log("func1", typeof arg1);
},
func2: function (arg1, arg2) {
console.log("func2", typeof arg1);
},
C#:
await jsRuntime.InvokeVoidAsync("utils.func1", null);
await jsRuntime.InvokeVoidAsync("utils.func1");
await jsRuntime.InvokeVoidAsync("utils.func1", "aaa");
await jsRuntime.InvokeVoidAsync("utils.func2", null, 123);
await jsRuntime.InvokeVoidAsync("utils.func2", "aaa", 123);
输出:
func1 undefined
func1 undefined
func1 string
func2 object
func2 string
【问题讨论】:
-
你试过
await jsRuntime.InvokeAsync<long>("func")吗? -
无复制。你可以发布 JS func() 吗?并告诉我们您使用的是什么版本。
-
已编辑:添加了一个重现问题的示例。当然,可以省略最后的参数以在 JS 中获取
undefined。我的问题是中间论点未定义。
标签: javascript blazor blazor-webassembly