Angular 2:如何修复'无法读取属性" lastIndexOf"?

时间:2016-05-24 05:17:06

标签: angularjs typescript angular

试图让我的头围绕角度2并创建一个注入服务的plunker:

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

@Injectable()
export class WikipediaService {
    constructor(private jsonp: Jsonp) {}

  search (term: string) {
    var search = new URLSearchParams()
    search.set('action', 'opensearch');
    search.set('search', term);
    search.set('format', 'json');
    return this.jsonp
                .get('http://en.wikipedia.org/w/api.php?callback=JSONP_CALLBACK', { search })
                .map((request) => request.json()[1]);
  }
}

此服务正在另一个名为FriendsList的类中使用:

import {Component,View,CORE_DIRECTIVES} from 'angular2/core';
import {Http, Response,HTTP_PROVIDERS} from 'angular2/http';
import 'rxjs/Rx';
 import {WikipediaService} from 'src/service',
import {Control} from 'angular2/common';
import 'rxjs/add/operator/map';
import 'rxjs/add/operator/debounceTime';
import 'rxjs/add/operator/distinctUntilChanged';
import 'rxjs/add/operator/switchMap';
import {Observable} from 'rxjs/Observable';


@Component({
    template: `
    <h1>artists</h1>
    <div>
      <h2>Wikipedia Search</h2>
      <input type="text" [ngFormControl]="term"/>
      <ul>
        <li *ngFor="#item of items | async">{{item}}</li>
      </ul>
    </div>
    `,
    directive:[CORE_DIRECTIVES]
  })

  export class FriendsList{
       items: Observable<Array<string>>;
       term = new Control();

      constructor(private wikipediaService: WikipediaService) {
      this.items = this.term.valueChanges
                .debounceTime(400)
                .distinctUntilChanged()
                .switchMap(term => this.wikipediaService.search(term));
  }

  }

控制台中的错误是这样的,我该如何解决这个问题? :

Error: Cannot read property 'lastIndexOf' of undefined
    Error loading http://run.plnkr.co/fGkpQYXMc0eGUy6e/src/myfriends.ts as "src/myfriends" from http://run.plnkr.co/fGkpQYXMc0eGUy6e/src/boot.ts
    at l.k (https://rawgit.com/systemjs/systemjs/0.19.6/dist/system.js:4:3324)
    at l.normalize (https://rawgit.com/systemjs/systemjs/0.19.6/dist/system.js:5:16628)
    at l.<anonymous> (https://rawgit.com/systemjs/systemjs/0.19.6/dist/system.js:5:3042)
    at l.<anonymous> (https://rawgit.com/systemjs/systemjs/0.19.6/dist/system.js:5:6587)
    at l.<anonymous> (https://rawgit.com/systemjs/systemjs/0.19.6/dist/system.js:5:10222)
    at l.<anonymous> (https://rawgit.com/systemjs/systemjs/0.19.6/dist/system.js:5:14003)
    at l.instantiate (https://rawgit.com/systemjs/systemjs/0.19.6/dist/system.js:5:16398)
    at https://rawgit.com/systemjs/systemjs/0.19.6/dist/system.js:4:5994
    at Zone.run (https://cdnjs.cloudflare.com/ajax/libs/angular.js/2.0.0-beta.0/angular2-polyfills.js:138:17)
    at zoneBoundFn (https://cdnjs.cloudflare.com/ajax/libs/angular.js/2.0.0-beta.0/angular2-polyfills.js:111:19)

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

1 个答案:

答案 0 :(得分:1)

你的myfriends.ts文件中有一个拼写错误:

import {WikipediaService} from 'src/service',

您需要使用;代替,,如下所示:

import {WikipediaService} from 'src/service';