【问题标题】:I get a error while publishing a app. The error is property json does not exist on type object发布应用程序时出现错误。错误是类型对象上不存在属性 json
【发布时间】:2018-06-14 06:50:52
【问题描述】:

当我尝试在 Ionic serve 命令中测试应用程序时,我没有收到任何错误。但是当我尝试发布应用程序时,我收到错误,因为“类型对象上不存在属性 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;

        });

        });

      }

    }

Weather.ts

    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() );

      }
    }

【问题讨论】:

  • 你在用 angular 5 吗?
  • 我不知道兄弟刚刚从这个link学习教程
  • 查看您的 package.json 并查看您使用的版本。
  • 是的,我正在使用 Angular 5.0.3 版本
  • @halfer ok halfer

标签: json angular typescript ionic-framework ionic3


【解决方案1】:

Typescript 就是关于打字的。因此,您应该说明您从方法 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;

        });
        });
      }

【讨论】:

  • 其中有一个。我返回一个 json 对象。如何在类中调用字符串?
  • 我不记得我为什么做出这个决定,但如果你返回一个对象而不是一个字符串,只需添加一个像这样的类型 current_observation: any;
  • 另一个。我打算用 Ionic 开发一个室内导航应用程序。如何使用 Ionic 存储将 Map 存储在本地并创建一个搜索 Map 的搜索按钮。有没有我可以使用的搜索 API?
【解决方案2】:

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 => { 让天气:任何=结果; this.weather = weather.current_observation; 控制台.log(this.weather); }); }); }

【讨论】:

  • 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 = 结果; this.weather = weather.current_observation; console.log(this.weather); }); }); }
  • 有一个编辑按钮,您可以使用它来提高答案质量。干杯!
猜你喜欢
  • 2020-10-18
  • 2021-10-26
  • 2018-07-16
  • 2018-10-21
  • 2018-05-01
  • 2020-08-07
  • 2019-03-31
  • 1970-01-01
  • 2022-07-01
相关资源
最近更新 更多