【问题标题】:Bad import in TypescriptTypescript 中的错误导入
【发布时间】:2020-12-03 19:35:09
【问题描述】:

我安装了这个包:https://www.npmjs.com/package/@types/googlemaps,创建一个从谷歌地图获取旅行时间的函数,但启动后,我的控制台抛出了我:

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

感谢您的帮助

【问题讨论】:

标签: javascript typescript google-maps google-maps-api-3


【解决方案1】:

尝试安装googlemaps的类型。

npm install --save @types/googlemaps

然后再试一次

import google from 'googlemaps';

import * as google from 'googlemaps';

【讨论】:

    猜你喜欢
    • 2017-03-20
    • 2020-12-18
    • 2015-07-19
    • 2017-06-17
    • 2021-07-03
    • 1970-01-01
    • 1970-01-01
    • 2016-01-02
    • 2019-07-07
    相关资源
    最近更新 更多