[英]Error: "Property '.get' doesn't exist on type 'JSON' (HTML / Javascript / Angular2 / Typescript)
Screenshot of error: 错误的屏幕截图:
Code where error exists: 存在错误的代码:
import {Component, OnInit} from 'angular2/core';
import {Router} from 'angular2/router';
import {Hero} from './hero';
import {HeroService} from './hero.service';
import {RouteConfig, ROUTER_DIRECTIVES} from 'angular2/router';
import {HeroesComponent} from './heroes.component';
import {HeroDetailComponent} from './hero-detail.component';
import {DashboardComponent} from './dashboard.component';
import {SpreadSheetComponent} from './spreadsheeteditall.component';
import {SwitchUsersComponent} from './SwitchUsers.component';
import {BiddingPageComponent} from './BiddingPage.component';
import { Injectable } from 'angular2/core';
import { Jsonp, URLSearchParams } from 'angular2/http';
@Component({
selector: 'SearchAndDisplayComponent',
templateUrl: 'app/SearchDisplay.component.html',
styleUrls: ['app/SearchDisplay.component.css'],
providers: [HeroService],
directives: [ROUTER_DIRECTIVES]
})
@Injectable()
export class SearchAndDisplayComponent{
constructor(private jsonp: JSON) {}
search (term: string) {
// let ebayURL = 'http://en.wikipedia.org/w/api.php';
var params = new URLSearchParams();
params.set('search', term); // the user's search value
params.set('action', 'opensearch');
params.set('format', 'json');
params.set('callback', 'JSONP_CALLBACK');
// TODO: Add error handling
return this.jsonp
.get({ search: params })
.map(request => <string[]> request.json()[1]);
}
}
Context of the problem: I am trying to create a search bar for a website that is basically a clone of ebay. 问题的背景:我正在尝试为基本上是eBay克隆的网站创建搜索栏。
Here is a question I posted earlier with links to the whole project (plunker/full project zipped) 这是我之前发布的一个问题,带有指向整个项目的链接(压缩了unkunk / full project)
Search bar that hides results that aren't typed into it 搜索栏可隐藏未键入的结果
HTML code of how I'm trying to display it by the click of a button next to the search bar: 单击搜索栏旁边的按钮,以显示我如何尝试显示的HTML代码:
<button (click)="search(term)">Search</button>
You need to inject the Jsonp
class instead of the JSON
one in the constructor. 您需要在构造函数中注入Jsonp
类而不是JSON
类。 The Jsonp object will allow you to execute JSONP requests. Jsonp对象将允许您执行JSONP请求。
import {Jsonp} from 'angular2/http';
(...)
export class SearchAndDisplayComponent{
constructor(private jsonp: Jsonp) {} // <-----
(...)
}
JSON is javascript namespace object. JSON是javascript名称空间对象。 You have imported Jsonp from the module angular2/http, so in constructor of yout service change JSON to Jsonp. 您已经从模块angular2 / http中导入了Jsonp,因此在yout服务的构造函数中将JSON更改为Jsonp。 And don't forget to add Jsonp provider to some component above at component hierarchy. 并且不要忘记在组件层次结构中将Jsonp提供程序添加到上面的某个组件中。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.