Typescript中导入错误

问题描述

我安装了此软件包:https://www.npmjs.com/package/@types/googlemaps,以创建一个功能获取Google地图的旅行时间,但是启动后,我的控制台抛出了我:

Error: Cannot find module 'googlemaps'

我尝试这样导入:

import * as google from 'googlemaps';

但不起作用,

我还尝试创建index.d.ts

declare module 'googlemaps';

但没有成功

我的代码

var google = require('googlemaps'); // another try


  async calculateTravelTime() {
    const directionsService = new google.maps.DirectionsService();
    
    directionsService.route(
        {
            origin: { lat: 37.77,lng: -122.447 },destination: { lat: 37.768,lng: -122.511 },travelMode: google.maps.TravelMode.DRIVING
        },(response,status) => {
            if(status == "OK") {
                console.log(response);
            } else {
                console.log(response);
            }
          }
      )
  }

那么,如何在打字稿中正确导入googlemaps

感谢您的帮助

解决方法

尝试安装googlemaps类型。

npm install --save @types/googlemaps

然后重试

import google from 'googlemaps';

import * as google from 'googlemaps';