简体   繁体   English

错误:“'JSON'类型不存在属性'.get'(HTML / Javascript / Angular2 / Typescript)

[英]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.

相关问题 Angular2和Typescript-类型上不存在属性名称 - Angular2 & Typescript - Property name does not exist on type Angular2打字稿错误TS2339:类型“ Y”上不存在属性“ x” - Angular2 typescript error TS2339: Property 'x' does not exist on type 'Y' 登录页面错误:“ http”不存在(HTML / TypeScript / Angular2 / Javascript) - Login page error: 'http' does not exist (HTML / TypeScript / Angular2 / Javascript) 类型&#39;any []&#39;(JSON)上不存在Angular2属性 - Angular2 Property does not exist on type 'any[]' (JSON) 类型上不存在属性:为什么 TypeScript 抱怨而 JavaScript 不抱怨? - Property does not exist on type: Why does TypeScript complain while JavaScript doesn't? '错误:未捕获(承诺):没有Jsonp的提供者'(HTML / Javascript / Typescript / Angular2) - 'Error: Uncaught (in promise): No provider for Jsonp' (HTML / Javascript / Typescript / Angular2) msSaveOrOpenBlob 属性在 Navigator 类型上不存在 angular 13 - msSaveOrOpenBlob property doesn't exist on type Navigator angular 13 Angular2 + Typescript + FileReader.onLoad =属性不存在 - Angular2 + Typescript + FileReader.onLoad = property does not exist Angular2 map - &gt; subscribe数据类型错误(类型&#39;ErrorObservable&#39;上不存在属性&#39;length&#39;。) - Angular2 map -> subscribe data type error ( Property 'length' does not exist on type 'ErrorObservable'.) 当 json 属性在 Javascript 中不存在时,为它添加一个默认值 - Add a default value for a json property when it doesn't exist in Javascript
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM