如何修复角度2错误无法读取属性' getOptional'?

时间:2016-05-24 03:37:11

标签: angularjs typescript angular

我正在考虑角度2并试图定义一个名为searchservice的服务并将其注入bootstap部分:

import {SearchService} from 'src/service';

这就是服务的样子:

import {Injectable} from 'angular2/core';
import {URLSearchParams, Jsonp} from 'angular2/http';

@Injectable()
export class SearchService {
  constructor(private http: Http) {}

  search (name: string) {
        http.get('https://api.spotify.com/v1/search?q='+name.value+'&type=artist')
        .map(response=>response.json());
  }
}

但是,我如何修复运行应用程序时出现的错误?:

VM337 angular2-polyfills.js:138 Error: Cannot read property 'getOptional' of undefined
    Error loading http://run.plnkr.co/fGkpQYXMc0eGUy6e/src/boot.ts
    at _runAppInitializers (https://cdnjs.cloudflare.com/ajax/libs/angular.js/2.0.0-beta.0/angular2.dev.js:14832:25)
    at PlatformRef_._initApp (https://cdnjs.cloudflare.com/ajax/libs/angular.js/2.0.0-beta.0/angular2.dev.js:14813:7)
    at PlatformRef_.application (https://cdnjs.cloudflare.com/ajax/libs/angular.js/2.0.0-beta.0/angular2.dev.js:14768:22)
    at Object.bootstrap (https://cdnjs.cloudflare.com/ajax/libs/angular.js/2.0.0-beta.0/angular2.dev.js:25054:64)
    at execute (http://run.plnkr.co/fGkpQYXMc0eGUy6e/src/boot.ts!transpiled:60:23)
    at u (https://rawgit.com/systemjs/systemjs/0.19.6/dist/system.js:5:97)
    at Object.execute (https://rawgit.com/systemjs/systemjs/0.19.6/dist/system.js:5:3188)
    at y (https://rawgit.com/systemjs/systemjs/0.19.6/dist/system.js:4:9948)
    at w (https://rawgit.com/systemjs/systemjs/0.19.6/dist/system.js:4:10327)

plunkr ref:http://plnkr.co/edit/F6TSGfyRnR5jvbpiv2QJ?p=preview

1 个答案:

答案 0 :(得分:0)

错误的重要部分是您列出的行之上的行,其中包含:

  

无法解析SearchService的所有参数(?)。确保它们都有有效的类型或注释。

您的搜索服务正在尝试定义Http类型的变量,但您没有将Http导入服务。

将导入行更改为import {URLSearchParams, Jsonp, Http} from 'angular2/http';