【发布时间】:2021-10-01 23:29:00
【问题描述】:
我想从我的 ASP.NET Core Blazor 应用程序调用的 JavaScript 函数:
export function onClick(map) {
var latLng = null;
function onMapClick(e) {
latLng = map.unproject(e.point).toString();
alert("You clicked the map at " + latLng); // Here is "You clicked the map at + actual value of latLng"
}
map.on('click', onMapClick);
alert("You clicked the map at 123 " + latLng); // And then "You clicked the map at + null" instead of latLng variable
return String(yourGlobalVariable);
}
我正在调用该函数的 C# 代码:
string res = await mapModule.InvokeAsync<string>("onClick", mapInstance);
其中 mapInstance 和 mapModule 是 IJSObjectReference
问题是:为什么在第一个警报中我得到一个实际值,我已经分配给函数 onMapClick 中的 latLng 变量,然后我在函数外部得到 null,尽管该变量是全局的?
我在这里尝试过类似的代码:http://jsfiddle.net/mufjkv6r/1/,它可以工作
【问题讨论】:
-
js模块是怎么设置的?我知道现在在 Blazor 中应该可以实现,但看起来并不简单。
-
@HenkHolteman, mapModule = await JS.InvokeAsync
("import", "./mapComponent.js"); - 像这样微不足道它来自示例:docs.microsoft.com/ru-ru/aspnet/core/blazor/…
标签: javascript c# blazor blazor-webassembly