简体   繁体   English

如何在angular 7中使用npm软件包('latlon-geohash')

[英]How to use npm package ('latlon-geohash') in angular 7

I'm trying to use the latlon-geohash npm package in my Angular 7 app, but when I run it, I get this error... 我正在尝试在Angular 7应用程序中使用latlon-geohash npm软件包,但是当我运行它时,出现此错误...

ERROR TypeError: latlon_geohash__WEBPACK_IMPORTED_MODULE_8__.encode is not a function 错误类型错误:latlon_geohash__WEBPACK_IMPORTED_MODULE_8 __。encode不是一个函数

Here is my code: 这是我的代码:

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))
  }

}

And it doesn't work. 而且它不起作用。 However, when I run console.log(gh) , I get this... 但是,当我运行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

I see my encode function right there. 我在那里看到了我的encode功能。 Why doesn't it work? 为什么不起作用? I can't seem to find an answer, but I feel like there may be something really simple I'm missing. 我似乎找不到答案,但是我觉得可能缺少一些非常简单的东西。 Any ideas? 有任何想法吗?

You should add the latlon-geohash to scripts inside angular.json . 您应该将添加latlon-geohash到脚本里面angular.json Any of the 3rd party scripts/libraries need to be made available inside scripts in angular.json . 任何第三方脚本/库都需要在angular.json scriptsangular.json Provide the full relative path of latlon-geohash.js to the angular project so that it becomes available for use. 提供latlon-geohash.js到angular项目的完整相对路径,以使其可供使用。

Make sure you Rebuild your project too. 确保您也重建项目。

EX: EX:

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

Also add the import to your component: import Geohash from 'latlon-geohash'; import Geohash from 'latlon-geohash';添加到您的组件中: import Geohash from 'latlon-geohash'; as mentioned by Tony. 正如Tony所提到的。

here is a stackblitz link for you to try 这是一个stackblitz链接供您尝试

Try these 2 way to see if it work 尝试以下两种方法,看看是否有效

import Geohash from 'latlon-geohash';

or 要么

const Geohash = require('latlon-geohash')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM