Bing Maps V8 TypeScript 包 - Microsoft 未定义
Bing Maps V8 TypeScript package - Microsoft is not defined
我想在我的 React (typescript) 项目中使用 BingMaps。
我安装了 npm bingmaps 包,它带来了必要的 d.ts.
我通过以下方式导入模块:
import 'bingmaps';
在我的组件中,我执行以下操作:
public componentDidMount(){
var map = new Microsoft.Maps.Map('#map',
{
credentials: 'XXXXX',
maxZoom: 6,
minZoom: 2,
});
};
此时我的应用抛出以下错误:
Microsoft.AspNetCore.NodeServices.HostingModels.NodeInvocationException: Prerendering failed because of error: ReferenceError: Microsoft is not defined
如何正确导入 BingMaps 以包括所有必要的 namespaces/modules?
我之前不熟悉 Bing 地图或 ASP.NET JavaScript 预渲染,但这是我的研究结果。 bingmaps
package just has the type declarations. The actual implementation comes in a JavaScript file (see documentation),我猜这个实现与预渲染不兼容。所以你需要:
通过从您的视图中删除 documentation 中描述的 asp-prerender-*
属性来完全禁用预渲染,或者
通过在 here.
描述的代码周围放置类似 if (typeof window !== 'undefined')
的测试,只为这个 React 组件跳过预渲染
我想在我的 React (typescript) 项目中使用 BingMaps。 我安装了 npm bingmaps 包,它带来了必要的 d.ts.
我通过以下方式导入模块:
import 'bingmaps';
在我的组件中,我执行以下操作:
public componentDidMount(){
var map = new Microsoft.Maps.Map('#map',
{
credentials: 'XXXXX',
maxZoom: 6,
minZoom: 2,
});
};
此时我的应用抛出以下错误:
Microsoft.AspNetCore.NodeServices.HostingModels.NodeInvocationException: Prerendering failed because of error: ReferenceError: Microsoft is not defined
如何正确导入 BingMaps 以包括所有必要的 namespaces/modules?
我之前不熟悉 Bing 地图或 ASP.NET JavaScript 预渲染,但这是我的研究结果。 bingmaps
package just has the type declarations. The actual implementation comes in a JavaScript file (see documentation),我猜这个实现与预渲染不兼容。所以你需要:
通过从您的视图中删除 documentation 中描述的
asp-prerender-*
属性来完全禁用预渲染,或者通过在 here.
描述的代码周围放置类似
if (typeof window !== 'undefined')
的测试,只为这个 React 组件跳过预渲染