繁体   English   中英

发布应用程序时出现错误。 错误是属性json在类型对象上不存在

[英]I get a error while publishing a app. The error is property json does not exist on type object

当我尝试在Ionic serve命令中测试应用程序时,没有收到任何错误。 但是,当我尝试发布应用程序时,出现错误,因为“类型object上不存在属性json”。 该错误发生在转换阶段:

我正在得到什么

如何解决这个问题呢? 我尝试了所有可能的方法,但是并没有解决我的问题。

Home.ts

import { Component } from '@angular/core';
import { NavController } from 'ionic-angular';
import { WeatherProvider } from '../../providers/weather/weather';
import { Storage } from '@ionic/storage';
//import { Response } from '@angular/http';
//import 'rxjs/add/operator/map';

@Component({
   selector: 'page-home',
   templateUrl: 'home.html'
})
export class HomePage {
   weather:any;
   location:{
        city:string,
        state:string
    }

   constructor(
      public navCtrl: NavController, 
      private weatherProvider:WeatherProvider,
      private storage: Storage) {

      }

      ionViewWillEnter(){
        this.storage.get('location').then((val)=>{
            if(val!=null){
                this.location = JSON.parse(val);
            }else{
                this.location = {
                    city: 'Chennai',
                    state: 'TN'
                }
            }


        this.weatherProvider.getWeather(this.location.city,this.location.state)
       // .map((res: Response) => res.json() )
            .subscribe(weather => {
                this.weather =  weather.current_observation;

        });

        });

      }

    }

天气

    import { HttpClient } from '@angular/common/http';
    import { Injectable } from '@angular/core';
    //import { Response } from '@angular/http';
    //import 'rxjs/add/operator/map';
    //import 'rxjs/Rx';

    @Injectable()
    export class WeatherProvider {
        apiKey = '6d3243fb22b01d0c';
        url;

      constructor(public http: HttpClient) {
        console.log('Hello WeatherProvider Provider');
        this.url = 'http://api.wunderground.com/api/'+this.apiKey+'/conditions/q';
      }
      getWeather(city, state){
        return this.http.get(this.url+'/'+state+'/'+city+'.json')
            // .map(res => res.json());
        // .map((res: Response) => res.json() );

      }
    }

打字稿就是关于打字的。 因此,您应该声明从getWeather方法接收的对象的类型。 首先在home.ts的末尾创建一个Weather类(请看下面)

class Weather {
  current_observation: string;
}

并进行以下更改:

this.weatherProvider.getWeather(this.location.city,this.location.state)
       // .map((res: Response) => res.json() )
            .subscribe((weather: Weather) => {
                this.weather =  weather.current_observation;

        });
        });
      }

ionViewWillEnter(){

this.storage.get('location').then((val) => {
  if(val != null){
    this.location = JSON.parse(val);
  } else{
    this.location ={
      city: 'miami',
      state: 'FL'
    }
  }

//尝试下面的代码this.weatherprovider.getweather(this.location.city,this.location.state).subscribe(result => {let weather:any = result; this.weather = weather.current_observation; console.log(this .weather);}); }); }

Angular 2:“可观察”类型上不存在错误属性“地图”<object> '.ts(2339)<div id="text_translate"><p> 我尝试编写 function 但不知何故弹出错误:当我想要时。map (res =&gt; res.json()); 我在这里做错了什么? 我使用最新的 Angular 版本。</p><pre> import { Injectable } from '@angular/core'; //import {Http, Headers} from '@angular/http'; import { HttpClient, HttpHeaders } from '@angular/common/http'; import {Contact} from './contact'; import 'rxjs/add/operator/map'; @Injectable({ providedIn: 'root' }) export class ContactService { constructor(private http: HttpClient) { } //retrieving ContactService getContacts() { return this.http.get('http://localhost:3000/api/contacts').map(res =&gt; res.json()); } //add contact method addContact(newContact) { var headers = new HttpHeaders(); headers.append('Content-Type', 'application/json'); return this.http.post('http://localhost:3000/api/contact', newContact, {headers:headers}).map(res =&gt; res.json()); } //delete methods deleteContact(id) { return this.http.delete('http://localhost:3000/api/contact'+ id).map(res =&gt; res.json()); } }</pre></div></object>

[英]Angular 2 : Error Property 'map' does not exist on type 'Observable<Object>'.ts(2339)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 错误TS2339:类型“对象”上不存在属性“ json” 在 Angular 中,如何避免“类型 &#39;Object&#39; 上不存在属性 &#39;json&#39;”错误? responseType:“json”标头导致错误:“对象”类型上不存在属性“responseStatus” this.http.get(&#39;... / contacts.json&#39;)-类型“ observable”不存在属性“ map” <object> “ 角度:错误 TS2339:“对象”类型上不存在“数据”属性 Angular 2:“可观察”类型上不存在错误属性“地图”<object> '.ts(2339)<div id="text_translate"><p> 我尝试编写 function 但不知何故弹出错误:当我想要时。map (res =&gt; res.json()); 我在这里做错了什么? 我使用最新的 Angular 版本。</p><pre> import { Injectable } from '@angular/core'; //import {Http, Headers} from '@angular/http'; import { HttpClient, HttpHeaders } from '@angular/common/http'; import {Contact} from './contact'; import 'rxjs/add/operator/map'; @Injectable({ providedIn: 'root' }) export class ContactService { constructor(private http: HttpClient) { } //retrieving ContactService getContacts() { return this.http.get('http://localhost:3000/api/contacts').map(res =&gt; res.json()); } //add contact method addContact(newContact) { var headers = new HttpHeaders(); headers.append('Content-Type', 'application/json'); return this.http.post('http://localhost:3000/api/contact', newContact, {headers:headers}).map(res =&gt; res.json()); } //delete methods deleteContact(id) { return this.http.delete('http://localhost:3000/api/contact'+ id).map(res =&gt; res.json()); } }</pre></div></object> 错误 TS2339:“响应”类型上不存在属性“结果” 错误 TS2339:“特殊 []”类型上不存在属性“默认” “字符串”JSON 类型上不存在属性“url” “类型 {} 上不存在属性 json” TypeScript
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM