VUE实现天气预报查询

vue实现天气预报查询功能

1、vue

 <div id="app">
    <div class="search">
      <input type="text" placeholder="请输入地区名称" v-model="city" @keyup.enter="searchWeather">
      <button @click="searchWeather">搜索天气</button>
      <div>
        <a @click="changeCity('北京市')">北京市</a>
        <a @click="changeCity('武汉市')">武汉市</a>
        <a @click="changeCity('杭州市')">杭州市</a>
        <a @click="changeCity('西安市')">西安市</a>
        <a @click="changeCity('福州市')">福州市</a>
        <a @click="changeCity('银川市')">银川市</a>
      </div>
    </div>
    <div class="weather" v-for="(item,index) in object.forecasts" :key="index">
<!--      当天的天气预报-->
      <div class="wea">
        <span>城市:{{item.city}}</span>
        <span>省份:{{item.province}}</span>
        <span>时间:{{item.reporttime}}</span>
      </div>
<!--      多天天气列表-->
      <div class="weather_box">
      <div class="wea_list" v-for="(wea,index) in item.casts" :key="index">
        <div class="wea_box">
          <div class="date_box">{{wea.date}}</div>
          <div class="week_box">{{wea.week}}</div>
          <div class="dayw_box">{{wea.dayweather}}</div>
          <div class="nigtw_box">{{wea.nightweather}}</div>
          <div class="temp_box">{{wea.daytemp}}~{{wea.nighttemp}}</div>
          <div class="wind_box">{{wea.daywind}}~{{wea.nightwind}}</div>
          <div class="power_box">{{wea.daypower}}~{{wea.nightpower}}</div>
          <div class="temp_box">{{wea.daytemp_float}}~{{wea.nighttemp_float}}</div>
        </div>
      </div>
    </div>
    </div>

  </div>

2、script

 data:function () {
    return{
      city: "",
      object:{
        forecasts:[]
      },
    }
  },
    methods:{
    searchWeather:function(){
      if(this.city.length == 0){
        alert("请输入城市");
        return;
      }//else{ //https://wthrcdn.etouch.cn/weather_mini?city="https://restapi.amap.com/v3/weather/weatherInfo?city=&key=<e0c48eb92615d1ab98b5ad25d3c6a044>"+this.city
        let that = this;
        this.$axios({
          url: "https://restapi.amap.com/v3/weather/weatherInfo",
          method: "GET",
          params: {
            key: "e0c48eb92615d1ab98b5ad25d3c6a044",
            city: this.city,
            extensions: 'all',
            // output: "JSON"
          },
        }).then(function(response){
              console.log("查询到的天气哪去了"+response.data)

          that.object = response.data;
          console.log(that.object);
            })
            .catch(function(error){
              alert("请求失败 " + error);
            })
    },
    changeCity:function(city){
      this.city = city;
      this.searchWeather();
    }
  }
  • 7
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值