vue3+uniapp is loaded when the applet is loaded for the first time or the cache does not exist. In other cases, it is not loaded and the cached data is directly called.

Enter the mini program for the first time to obtain weather data and store it. At other times, return to this page without sending a request and directly call the cached data.

  import { ref } from "vue"

  const weather = ref({
        'temperature': '24',
        'weather': '晴',
        'city': '未知',
        'reporttime': ''
    })

    
   onLoad(() => {
        const weatherInfo = uni.getStorageSync('weather')
        //每次进入小程序都打开或缓存不存在时打开,其他情况不打开
        let pages = getCurrentPages();
        if (pages.length <= 1 || !weatherInfo) {
            //缓存不存在,获取天气定位
            uni.getLocation({
                type: 'wgs84',
                success: function (res) {
                    getWeather({ "longitude": res.longitude, "latitude": res.latitude }).then((res : any) => {
                        weather.value = res.data
                        uni.setStorageSync('weather', weather)
                    }).catch((res : any) => {
                        console.log(res, '天气接口处理失败')
                    })
                }
            });

        }
    })  

Guess you like

Origin blog.csdn.net/LJM51200/article/details/134188251