无法从Angular2中的node-modules文件夹导入ts文件

时间:2016-08-09 15:53:49

标签: angular import google-places-api directive

我已在Angular 2项目中安装了Google Map位置。

npm install angular2-google-map-auto-complete

enter image description here

获取错误,因为找不到模块:

enter image description here

有人可以帮我指定相同的路径吗?

尝试以下组合:

../../directives/googleplace.directive
../../../directives/googleplace.directive
../../../../directives/googleplace.directive
../../directives/googleplace.directive
../node-modules/angular2-google-map-auto-complete/directives/googleplace.directive
../../angular2-google-map-auto-complete/directives/googleplace.directive

没有按预期工作。

更新:我发现拼写错误但没有运气

enter image description here

1 个答案:

答案 0 :(得分:2)

试试这个 -

import {GoogleplaceDirective} from '../directives/googleplace.directive';

[请注意 - 其指令 s 与' s '最后。]

并在您的组件中添加如下指令 -

directives: [GoogleplaceDirective],

看看这是否有帮助。

修改

你可以这样使用 -

import {GoogleplaceDirective} from '../node_modules/angular2-google-map-auto-complete/directives/googleplace.directive';

在VScode中验证 -

VScode screenshot

编辑2:

Step1:复制googleplace.directive.ts文件并将其保存在您的应用文件夹中

Folder strcture

Step2:像这样编写appcomponent.ts -

import { Component } from '@angular/core';
import {GoogleplaceDirective} from './googleplace.directive';

@Component({
  selector: 'my-app',
  directives: [GoogleplaceDirective],
  template: `<h1>My First Angular 2 App</h1>
  <input type="text" [(ngModel)] = "address"  (setAddress) = "getAddress($event)" googleplace/>
  `

})
export class AppComponent { 
  public address : Object;
       getAddress(place:Object) {       
           this.address = place['formatted_address'];
           var location = place['geometry']['location'];
           var lat =  location.lat();
           var lng = location.lng();
           console.log("Address Object", place);
       }
}

第3步:在index.html中添加脚本标记

<script src="https://maps.googleapis.com/maps/api/js?libraries=places&sensor=false"></script>

输出结果:

Output Result

希望这会有所帮助。