Angular2找不到名称空间"google" [英] Angular2 Cannot find namespace 'google'

查看:83
本文介绍了Angular2找不到名称空间"google"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用angular2-google-maps和最新版本的Angular2.我正在尝试将一些本地地图组件功能转换为它们自己的文件maps.service.ts中的服务.例如:

I am working with angular2-google-maps and latest version of Angular2. I am trying to convert some of the local map component functions into services in their own file maps.service.ts. For example:

map.component.ts

map.component.ts

getGeoLocation(lat: number, lng: number) {
if (navigator.geolocation) {
    let geocoder = new google.maps.Geocoder();
    let latlng = new google.maps.LatLng(lat, lng);
    let request = { latLng: latlng };
    geocoder.geocode(request, (results, status) => {
      if (status == google.maps.GeocoderStatus.OK) {
        let result = results[0];
        if (result != null) {
          this.fillInputs(result.formatted_address);
        } else {
          alert("No address available!");
        }
      }
    });
}
}

变成类似:maps.service.ts

getGeoLocation(lat: number, lng: number): Observable<google.maps.GeocoderResult[]> {
    let geocoder = new google.maps.Geocoder();
    let latlng = new google.maps.LatLng(lat, lng);
    let request = { latLng: latlng };
    return new Observable((observer: Observer<google.maps.GeocoderResult[]>) => {
        geocoder.geocode({ request }, (
            (results: google.maps.GeocoderResult[], status: google.maps.GeocoderStatus) => {
                if (status == google.maps.GeocoderStatus.OK) {
                    observer.next(results);
                    observer.complete();
                } else {
                    console.log('Geocoding service failed due to: ' +status);
                    observer.error(status);
                }
            }
        ));
    });
}

我遇到的问题是,当我尝试使用Observer<google.maps.GeocoderResult[]>时,无法识别google变量.我在服务类之外也有declare var google: any;.

The issue I'm getting is that google variable is not being recognized when I try to use Observer<google.maps.GeocoderResult[]>. I have declare var google: any; outside of the service class as well.

google变量在我的map.componenet.ts中可用,但在maps.service.ts中没有被识别.

The google variable works in my map.componenet.ts but doesn't get recognized in the maps.service.ts.

推荐答案

我终于弄清楚了这个问题,我不知道这是一个问题.我在其中引用服务的组件名为map.component.ts,而我的服务文件名为maps.service.ts,其中smap的末尾.在更改文件和import语句后,一切正常.

I finally figured out the problem, which I didn't know was a thing. My component that I was referencing the services in was named map.component.ts while my services file was named maps.service.ts with the s at the end of map. After I changed the file and import statements everything worked fine.

这篇关于Angular2找不到名称空间"google"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆