首页 > 解决方案 > 离子反应,正确代码的错误“类型参数不可分配”

问题描述

我有一些错误信息。对于排除代码问题,我在标准应用程序中发现了此错误。当我使用“空白”、“侧边菜单”、“标签”时,一切正常。但是当开始“会议”然后运行时,我有错误消息:

/home/stdt/src/components/Map.tsx(15,39) 中的 TypeScript 错误:
[react-scripts] 类型参数 'HTMLDivElement | null' 不可分配给“元素”类型的参数。[react-scripts] 类型“null”不可分配给类型“元素”。TS2345 [反应脚本]
13 | useEffect(() => { [react-scripts]
14 | [react-scripts]
15 | map.current = new google.maps.Map(mapEle.current, { [react-scripts] ^

其他一些应用程序中的类似问题。此代码来自示例,因此没有代码问题。我应该怎么做,安装一些东西或别的东西?

标签: reactjsionic-framework

解决方案


我曾经也有过一样的问题。我发现的两个“解决方案”是:

  1. 在 tsconfig.json 中添加一行:
    "strictNullChecks": false,
  1. 在 src/components/Map.tsx 地方if (mapEle && mapEle.current) {}周围使用 mapEle:
    if (mapEle && mapEle.current) {
       map.current = new google.maps.Map(mapEle.current, {
          center: {
            lat: mapCenter.lat,
            lng: mapCenter.lng
          },
          zoom: 16
       });


       addMarkers();

       google.maps.event.addListenerOnce(map.current, 'idle', () => {
         if (mapEle.current) {
           mapEle.current.classList.add('show-map');
         }
       });
     }

推荐阅读