gpt4 book ai didi

javascript - 如何在 Angular 7 中使用 npm 包 ('latlon-geohash' )

转载 作者:搜寻专家 更新时间:2023-10-30 21:20:59 26 4
gpt4 key购买 nike

我正在尝试使用 latlon-geohash npm 包在我的 Angular 7 应用程序中,但是当我运行它时,出现此错误...

ERROR TypeError: latlon_geohash__WEBPACK_IMPORTED_MODULE_8__.encode is not a function

这是我的代码:

import { Component, OnInit, Output, EventEmitter } from '@angular/core';
import * as gh from 'latlon-geohash';

@Component({
selector: 'app-account',
templateUrl: './account.component.html',
styleUrls: ['./account.component.scss'],
})
export class AccountComponent implements OnInit {

constructor() {

}

ngOnInit() {
//Here I'm trying to encode my lat and lng to a GeoHash
console.log(gh.encode(39.36, -76.69, 4))
}

}

而且它不起作用。但是,当我运行 console.log(gh) 时,我得到了这个...

Module {default: ƒ, __esModule: true, Symbol(Symbol.toStringTag): "Module"}
default: class Geohash
adjacent: ƒ adjacent(geohash, direction)
arguments: (...)
bounds: ƒ bounds(geohash)
caller: (...)
decode: ƒ decode(geohash)
encode: ƒ encode(lat, lon, precision) //HERE IS MY FUNCTION
length: 0
name: "Geohash"
neighbours: ƒ neighbours(geohash)
prototype: {constructor: ƒ}
__proto__: ƒ ()
[[FunctionLocation]]: latlon-geohash.js:11
[[Scopes]]: Scopes[3]
Symbol(Symbol.toStringTag): "Module"
__esModule: true
__proto__: Object

我在那里看到了我的encode 函数。为什么它不起作用?我似乎找不到答案,但我觉得我可能缺少一些非常简单的东西。有什么想法吗?

最佳答案

您应该将 latlon-geohash 添加到 angular.json 中的脚本中。任何第 3 方脚本/库都需要在 angular.jsonscripts 中可用。将 latlon-geohash.js 的完整相对路径提供给 Angular 项目,以便它可供使用。

确保您也重建您的项目。

例如:

"scripts": [
"./node_modules/path-to-lib/latlon-geohash.js",

同时将导入添加到您的组件:import Geohash from 'latlon-geohash'; 如 Tony 所述。

这是一个stackblitz link给你试试

关于javascript - 如何在 Angular 7 中使用 npm 包 ('latlon-geohash' ),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57097651/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com