vue实时显示当前时间和天气

推荐更简单的方式(使用dayjs):https://blog.csdn.net/qq_40323256/article/details/110930383 

一、时间

代码实现: 

  export default{
    data(){
      return{
        date:new Date()
      }
    },
    methods:{
      dateFormat(time) {
          var date=new Date(time);
          var year=date.getFullYear();
          /* 在日期格式中,月份是从0开始的,因此要加0
          * 使用三元表达式在小于10的前面加0,以达到格式统一  如 09:11:05
          * */
          var month= date.getMonth()+1<10 ? "0"+(date.getMonth()+1) : date.getMonth()+1;
          var day=date.getDate()<10 ? "0"+date.getDate() : date.getDate();
          var hours=date.getHours()<10 ? "0"+date.getHours() : date.getHours();
          var minutes=date.getMinutes()<10 ? "0"+date.getMinutes() : date.getMinutes();
          var seconds=date.getSeconds()<10 ? "0"+date.getSeconds() : date.getSeconds();
          // 拼接
          return year+"-"+month+"-"+day+" "+hours+":"+minutes+":"+seconds;
      }
    },
    mounted() {
          //显示当前日期时间
          let _this = this// 声明一个变量指向Vue实例this,保证作用域一致
          this.timer = setInterval(() => {
           _this.date = new Date(); // 修改数据date
           }, 1000)
       },
      beforeDestroy() {
       if (this.timer) {
        clearInterval(this.timer); // 在Vue实例销毁前,清除我们的定时器
      }
}

使用: 

   <div>{{dateFormat(date)}}</div>

效果图:

二、天气

mounted() {
  this.getWeather();
  this.timer = setInterval(() => {
  	this.getWeather();
  }, 1000 * 60 * 60)		
},
methods: {
  getWeather() { // 第三方天气api接口
    axios.get('https://www.tianqiapi.com/api/', {
      params: {
        appid: '26148275',
        appsecret: '2id6H48Y',
        version: 'v6'
      }
    }).then(res => {
      if (res.data) {
        if (res.data.wea_img == 'xue') {
          this.imgSrc = require('../assets/img/brand/xue.png');
        } else if (res.data.wea_img == 'yin') {
          this.imgSrc = require('../assets/img/brand/yin.png');
        } else if (res.data.wea_img == 'yu' || res.data.wea_img == 'bingbao') {
          this.imgSrc = require('../assets/img/brand/yu.png');
        } else if (res.data.wea_img == 'yun') {
          this.imgSrc = require('../assets/img/brand/yun.png');
        } else if (res.data.wea_img == 'wu') {
          this.imgSrc = require('../assets/img/brand/wu.png');
        } else if (res.data.wea_img == 'shachen') {
          this.imgSrc = require('../assets/img/brand/shachen.png');
        } else if (res.data.wea_img == 'lei') {
          this.imgSrc = require('../assets/img/brand/lei.png');
        } else {
          this.imgSrc = require('../assets/img/brand/qing.png');
        }
        this.weatcherData = res.data;
      }
    }).catch(err => {
      console.log(err)
    })
  }
}

 

  • 7
    点赞
  • 28
    收藏
    觉得还不错? 一键收藏
  • 7
    评论
为了在Vue3中显示实时天气,你可以使用第三方API来获取天气数据,并将其显示Vue组件中。以下是实现此目的的一些步骤: 1.注册一个免费的天气API服务,例如OpenWeatherMap或WeatherAPI。 2.在Vue组件中使用Axios或Fetch等库来获取天气数据。 3.将获取的数据绑定到Vue组件的模板中,以便将其显示给用户。 下面是一个简单的示例,演示如何在Vue3中显示实时天气: ```html <template> <div> <h1>{{ city }}天气</h1> <div v-if="loading">正在加载...</div> <div v-else> <p>温度:{{ temperature }}℃</p> <p>天气状况:{{ description }}</p> <p>湿度:{{ humidity }}%</p> <p>风速:{{ windSpeed }}米/秒</p> </div> </div> </template> <script> import axios from 'axios'; export default { name: 'Weather', data() { return { city: '北京', temperature: '', description: '', humidity: '', windSpeed: '', loading: true, }; }, created() { this.getWeather(); }, methods: { async getWeather() { try { const response = await axios.get( `https://api.openweathermap.org/data/2.5/weather?q=${this.city}&appid=YOUR_API_KEY&units=metric` ); this.temperature = response.data.main.temp; this.description = response.data.weather[0].description; this.humidity = response.data.main.humidity; this.windSpeed = response.data.wind.speed; this.loading = false; } catch (error) { console.error(error); } }, }, }; </script> ``` 在上面的示例中,我们使用了OpenWeatherMap API来获取天气数据,并将其绑定到Vue组件的模板中。请注意,你需要将YOUR_API_KEY替换为你自己的API密钥。
评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值