【发布时间】:2021-07-09 13:21:53
【问题描述】:
当我尝试在混合 blazor 应用程序中通过互操作调用名为 initMap 的 js 函数时,我不断收到以下错误:
在“窗口”中找不到“initMap”
我有以下代码:
TestApp.Windows\wwwroot\index.html
...
<head>
<script src="../js/initGoogleMap.js"></script>
<script type="text/javascript" src="https://maps.googleapis.com/maps/api/js?key=&callback=initMap"></script>
</head>
...
TestApp.Windows\wwwroot\js\initGoogleMap.js
window.initGoogleMap = {
initMap: function () {
const latLng = new google.maps.LatLng(40.716948, -74.003563);
const options = {
zoom: 14,
center: latLng,
mapTypeId: google.maps.MapTypeId.ROADMAP
}
const map = new google.maps.Map(document.getElementById("map"), options)
}
};
TestApp\WebUI\Pages\Index.razor
@page "/"
@inject IJSRuntime JSRuntime
<h1>Hello, world!</h1>
Welcome to your new app.
<div id="map" style="height:500px; width:100%;"></div>
@code{
protected override async Task OnAfterRenderAsync(bool firstRender) {
if (firstRender)
{
Console.WriteLine("first render");
await JSRuntime.InvokeVoidAsync("initMap");
}
}
}
任何想法为什么会发生此错误?我尝试了几个小调整,但无济于事。任何帮助将不胜感激!
【问题讨论】:
-
你的 js 路径有两个点和一个斜线。那是一个目录。与索引所在的位置相比,它实际上位于子目录中。
标签: javascript blazor