【问题标题】:Issue printing api weather data too a html page问题打印 api 天气数据太 html 页面
【发布时间】:2020-07-27 22:14:05
【问题描述】:

我也在尝试编写一个非常基本的天气应用程序,该应用程序从这个 API https://openweathermap.org/api 中提取数据。当我从 home.html 文件中单击链接时,weatherData.html 也没有显示任何内容,而且我似乎无法在我的代码中找到问题,任何帮助将不胜感激。

<ion-header [translucent]="true">
  <ion-toolbar>
    <ion-title>
      Weather Ireland
    </ion-title>
  </ion-toolbar>
</ion-header>

<ion-content [fullscreen]="true">
  <ion-header collapse="condense">
    <ion-toolbar>
      <ion-title size="large">Weather Ireland</ion-title>
    </ion-toolbar>
  </ion-header>

  <a class="button icon-right ion-plus-round" href="../weather-data/weather-data.page.html">weatherData</a>
</ion-content>

这是 homepage.html 的代码,它也链接了应该打印 API 信息的 weatherData.html 文件。

import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
import { FormsModule } from '@angular/forms';

import { IonicModule } from '@ionic/angular';

import {WeatherDataService} from '../weather-data.service'

import { WeatherDataPageRoutingModule } from './weather-data-routing.module';

import { WeatherDataPage } from './weather-data.page';



@NgModule({
  imports: [
    CommonModule,
    FormsModule,
    IonicModule,
    WeatherDataPageRoutingModule
  ],
  declarations: [WeatherDataPage]
})
export class WeatherDataPageModule {
  weather;

  constructor(private weatherdataservice: WeatherDataService){}
  ngOnInit(){
    this.weatherdataservice.getWeather().subscribe((data)=>{
      console.log(data);
      this.weather = data['weather'];
    }
    )
  }
}

这是 data.html 文件的代码

import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
import { FormsModule } from '@angular/forms';

import { IonicModule } from '@ionic/angular';

import {WeatherDataService} from '../weather-data.service'

import { WeatherDataPageRoutingModule } from './weather-data-routing.module';

import { WeatherDataPage } from './weather-data.page';



@NgModule({
  imports: [
    CommonModule,
    FormsModule,
    IonicModule,
    WeatherDataPageRoutingModule
  ],
  declarations: [WeatherDataPage]
})
export class WeatherDataPageModule {
  weather;

  constructor(private weatherdataservice: WeatherDataService){}
  ngOnInit(){
    this.weatherdataservice.getWeather().subscribe((data)=>{
      console.log(data);
      this.weather = data['weather'];
    }
    )
  }
}

这是 data.module.ts 文件的代码

import { Injectable } from '@angular/core';
import { HttpClient} from '@angular/common/http'


@Injectable({
  providedIn: 'root'
})
export class WeatherDataService {

  APIKey = 'fec30507acb533f670080ab3174f226f';


  constructor(private httpClient: HttpClient) { }

  public getWeather(){
  return this.httpClient.get('api.openweathermap.org/data/2.5/weather?lat={53.87}&lon={8.63}&appid={fec30507acb533f670080ab3174f226f}')
  }

}

这是 data.services ts 文件的代码。

【问题讨论】:

    标签: html typescript api ionic-framework weather-api


    【解决方案1】:

    从下面的代码中,您似乎想重定向到某个页面,尽管您有来自 api 的响应。尝试创建新页面并传递响应,而不是链接重定向。

        <ion-header [translucent]="true">
          <ion-toolbar>
            <ion-title>
              Weather Ireland
            </ion-title>
          </ion-toolbar>
        </ion-header>
    
        <ion-content [fullscreen]="true">
          <ion-header collapse="condense">
            <ion-toolbar>
              <ion-title size="large">Weather Ireland</ion-title>
            </ion-toolbar>
          </ion-header>
    
          **<a class="button icon-right ion-plus-round" href="../weather-data/weather-data.page.html">weatherData</a>**
    
        </ion-content>
    

    在 WeatherDataPage 中尝试使用 NavController 在构造函数中声明,navCtrl: NavController 并使用 this.navCtrl.push(component_name, {whetherData: this.weather});

    导航到新的组件页面使用后,NavParams。声明 navParams: NavParams 并使用 navParams.get("whetherData");

    【讨论】:

    • 在新文件中我是使用类型脚本文件还是模块文件
    • 构造函数(public navCtrl:NavController){} this.navCtrl.push(DataHolderPageModule),{WeatherDataPageModule:this.weather};使用这行代码,我也收到错误“意外的令牌。预期的构造函数、方法、访问器或属性。”
    • ERROR 错误:未捕获(在承诺中):错误:无法匹配任何路由。 URL 段:'weather-data/weather-data.page.html' 错误:无法匹配任何路由。 URL 段:'weather-data/weather-data.page.html' at ApplyRedirects.noMatchError (router.js:4295) at CatchSubscriber.selector (router.js:4259) at CatchSubscriber.error (catchError.js:29) at MapSubscriber._error (Subscriber.js:75) at MapSubscriber.error (Subscriber.js:55) at MapSubscriber._error (Subscriber.js:75) 这些是我在浏览器中单击检查元素时的一些错误
    • this.navCtrl.push(____),{WeatherDataPageModule:this.weather};在您的组件的空白放置名称中,例如“WhetherDataPageComponent”(您的新组件页面)
    猜你喜欢
    • 1970-01-01
    • 2014-08-05
    • 1970-01-01
    • 1970-01-01
    • 2011-07-30
    • 2021-08-07
    • 2012-04-07
    • 1970-01-01
    • 2019-05-28
    相关资源
    最近更新 更多