javascript - Typescript 中的错误导入
问题描述
我安装了这个包: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
打字稿?
谢谢你的帮助
解决方案
尝试安装 googlemaps 的类型。
npm install --save @types/googlemaps
然后再试一次
import google from 'googlemaps';
或者
import * as google from 'googlemaps';
推荐阅读
- hyperledger-fabric - 允许参与者完全读取权限
- vba - 在特定条件下保持单元格不变
- angularjs - angularjs-ui-tinymce 绑定不起作用
- xml - 如何使用 XSLT 为每个 html 选择设置唯一名称?
- database - 将 AWS DynamoDB 迁移到 Postgresql
- c# - 检查 Textbox.Text 上的多个条件的最佳方法
- c# - ModelState 是有效的,但是当我保存数据时,我在有错误的 entityValidationErrors 中得到它
- sql - 如何获取上周(7天)每天的记录数oracle
- javascript - Facebook 页面插件 - 响应式
- php - 如何将子字符串添加到以逗号分隔的字符串中的每个元素