Angular2错误'类型' {}'的争论是不可分配的......'

时间:2016-03-03 16:46:15

标签: angular

我试图练习这个例子plnkr这个例子效果很好但是在下面的代码中尝试做类似的事情

我收到此错误:

  

类型' {}'不能分配给' string'类型的参数。   (参数)术语:{}

此代码不等于100%的plunker,但如果它不起作用则不会因为我在下面的代码中使用时出错,或者在Angular2中更改了某些内容,或者是angular2的某些部分。< / p>

如果有人可以指导我。

app.component.ts

import { Component } from 'angular2/core';
import { NgFor } from 'angular2/common';

//inicio test Servicios ect
import {Control} from 'angular2/common';
import {JSONP_PROVIDERS} from 'angular2/http';
import {WikipediaService} from './servicioWiki';
import {Observable} from 'rxjs/Observable';
import 'rxjs/add/operator/map';
import 'rxjs/add/operator/debounceTime';
import 'rxjs/add/operator/distinctUntilChanged';
import 'rxjs/add/operator/switchMap';
//fin test Servicios ect

@Component({

    selector: "appinit",
    directives:[NgFor,WikipediaService, JSONP_PROVIDERS],
    template: `<div *ngFor="#movie of movies">
                   <h1> {{ movie }} Hello Angular 2</h1>
               </div>
               <div>
                <h2>Wikipedia Search</h2>
                    <input type="text" [ngFormControl]="term"/>
                    <ul>
                        <li *ngFor="#item of items | async">{{item}}</li>
                    </ul>
               </div>`   
})

export class AppComponent {

    movies: Array<any>;
    items: Observable<Array<string>>;
    term = new Control();

    constructor(private wikipediaService: WikipediaService){
        this.movies = ["movie1"];

        this.items = this.term.valueChanges
                     .debounceTime(400)
                     .distinctUntilChanged()
                     .switchMap(term => this.wikipediaService.search(term));
    }    
}

servicioWiki.ts

import {Injectable} from 'angular2/core';
import {URLSearchParams, Jsonp} 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]);
  }
}

package.json info

..//
"license": "ISC",
  "dependencies": {
    "angular2": "2.0.0-beta.7",
    "systemjs": "0.19.22",
    "es6-promise": "^3.0.2",
    "es6-shim": "^0.33.3",
    "reflect-metadata": "0.1.2",
    "rxjs": "5.0.0-beta.2",
    "zone.js": "0.5.15"
  },
  ..//

在这行代码中,在文件app.component.ts中, - &gt; .switchMap(term => this.wikipediaService.search(term));

term - &gt;

  

类型&#39; {}&#39;不能分配给&#39; string&#39;类型的参数。   (参数)术语:{}

1 个答案:

答案 0 :(得分:5)

实际上,您需要为switchMap运算符提供的回调参数指定类型:

constructor(private wikipediaService: WikipediaService) {
  this.movies = ["movie1"];

  this.items = this.term.valueChanges
        .debounceTime(400)
        .distinctUntilChanged()
        .switchMap((term:string) => this.wikipediaService.search(term)); // <------
}

这将在编译时删除错误。