vue天气预报

assets引入天气图片,用于图片代码拼接展示到页面上

 <div class="newsflash-title">天气预报</div>
      <div style="margin-bottom:50px">
        <span>切换城市:</span>
        <el-cascader
          placeholder="试试搜索:北京"
          :options="city"
          @change="handleCascaderChange"
          v-model="selectedValues"
          :props="{ checkStrictly: true,expandTrigger: 'hover'  }"
          filterable
          :show-all-levels="false"></el-cascader>
        <div v-if="weather" style="font-size: 40px;font-weight: bold;margin-top: 20px">{{weather.city}}</div>

        <div class="weather">
          <img style="width: 85px;height: 85px;" :src="require(`../assets/weather/${todaySWeather.dayweather}.png`)"/>
          <div class="weathertext">{{todaySWeather.dayweather}}</div>
          <div class="temperature" v-if="todaySWeather.daytemp">{{todaySWeather.nighttemp}}~{{todaySWeather.daytemp}}&#8451</div>
        </div>
        <div class="updateTime">更新时间:{{weather.reporttime}}</div>
        <div  class="_3Days">未来3天</div>
        <div class="future">
          <div v-for="(item,index) in casts" :key="index" >
            <div class="list-status">星期{{item.week | weekChinese}}</div>
            <div class="list-status">{{item.date | dayTime}}</div>
            <img  style="width: 45px;height: 45px;margin: 10px auto;" :src="require(`../assets/weather/${item.dayweather}.png`)"/>
            <div class="list-status">{{item.dayweather}}</div>
            <div class="list-temp">{{item.nighttemp}}~{{item.daytemp}}&#8451</div>
          </div>
        </div>
      </div>
.weather{
  margin: 20px 0;
  font-size: 45px;
  display: flex;
  color: #666;
  align-items: center;
  justify-content: start;
}
.weathertext{
  margin: 0 20px;
}
.future{
  width: 90%;
  display: flex;
  text-align: center;
  justify-content: space-between;
}
._3Days{
  margin: 20px 0;
  font-style: italic;
  font-size: 18px;
  font-weight: bold;
  color:#5f5f5f;
}
.updateTime{
  font-size: 16px;
  color: #999;
  margin-top: 10px;
}
.list-status {
  font-size: 14px;
  line-height: 19px;
  color: #222222;
}
.list-temp {
  font-size: 16px;
  font-weight: bold;
  padding-top: 8px;
}

data

引入城市文件

    weather: [],
      todaySWeather: [],//今天
      casts: [],//未来几天
      selectedValues: ["柳州"],
      city: JSON.parse(JSON.stringify(options)),
 filters:{
    filtertime(value) {
      return moment(value).format('YYYY-MM-DD HH:mm:ss');
    },
    weekChinese(value) {
      let week = '';
      switch (value) {
        case '1':
          week = '一';
          break;
        case '2':
          week = '二';
          break;
        case '3':
          week = '三';
          break;
        case '4':
          week = '四';
          break;
        case '5':
          week = '五';
          break;
        case '6':
          week = '六';
          break;
        case '7':
          week = '日';
          break;
      }
      return week;
    },
  dayTime(value) {
      return moment(value).format('MM/DD');
    }
  },
  methods: {
    handleCascaderChange() {
      console.log(this.selectedValues)
      this.selectedValues = this.selectedValues[this.selectedValues.length - 1];
      this.$axios({
        url: `https://restapi.amap.com/v3/weather/weatherInfo?city=${this.selectedValues}&key=高德地图申请的密钥&extensions=all`,
        method: 'get',
      }).then((res) => {
        const {forecasts}=res.data
        this.weather = forecasts[0]
        this.todaySWeather= forecasts[0].casts[0]
        this.casts = forecasts[0].casts.slice(1)
        console.log(this.casts,'weather')
      });
    },}
  • 2
    点赞
  • 26
    收藏
    觉得还不错? 一键收藏
  • 5
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值