获取当前的实时天气

当前地区位置的实时天气,具体代码如下

 <div class="weather">
    {{ weatherDescription }}&emsp;{{ temperature }}°C
 </div>

<script>
import axios from "axios";

export default {
  data() {
    return {
      temperature: "",
      weatherDescription: "",
    };
  },
  methods: {
    getWeather() {
      // 调用和风天气API,传入城市代码和API密钥
      axios
        .get("https://devapi.qweather.com/v7/weather/now", {
          params: {
            location: "CN101190107",
            key: "ad937e721eab42c0a39be80c121b25e2",
          },
        })
        .then((response) => {
          console.log(response);
          // 处理API响应,获取温度和天气情况
          const weatherData = response.data.now;
          const temperature = weatherData.temp;
          const weatherDescription = weatherData.text;

          // 将数据存储到组件状态中
          this.temperature = temperature;
          this.weatherDescription = weatherDescription;
        })
        .catch((error) => {
          console.log(error);
        });
    },
  },
  mounted() {
    let now = new Date();
    this.year = now.getFullYear();
    this.month = now.getMonth() + 1;
    this.day = now.getDate();
    // 七天请求一次数据
    // setInterval(() => {
    //   console.log(1111111111);
    //    axios
    //     .get("https://devapi.qweather.com/v7/weather/now", {
    //       params: {
    //         location: "CN101190107",
    //         key: "ad937e721eab42c0a39be80c121b25e2",
    //       },
    //     })
    //     .then((response) => {
    //       console.log(response);
    //       // 处理API响应,获取温度和天气情况
    //       const weatherData = response.data.now;
    //       const temperature = weatherData.temp;
    //       const weatherDescription = weatherData.text;

    //       // 将数据存储到组件状态中
    //       this.temperature = temperature;
    //       this.weatherDescription = weatherDescription;
    //     })
    //     .catch((error) => {
    //       console.log(error);
    //     });
    // }, 7 * 24 * 60 * 60 * 1000);
  },
  created() {
    this.getWeather();
    // setInterval(() => {
    //   this.currentTime = new Date().toLocaleTimeString();
    // }, 1000);
  },
};
</script>

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值