💎一站式轻松地调用各大LLM模型接口,支持GPT4、智谱、星火、月之暗面及文生图 广告
说明:当所选城市变化时相应的数据也要发生改变,而考虑到性能优化以将数据放缓存,发送不了请求,这时候就要activated()函数或者监听函数处理数据 ~~~ data() { return { swiperList: [], icon: [], icons: [], recommendList:[], weekendList:[], + currentCity:"" }; }, + computed:{ city(){ return this.$store.state.city } }, // mounted(){}也可以 created() { + this.currentCity=this.city homeModel.getlist(this.city).then(res => { var swiperList = res.data.gallary; var temp = res.data.icons; var recommendList = res.data.recommendList; var weekendList = res.data.weekendList; var icon = temp.slice(0, 8); var icons = temp.slice(8, 10); this.swiperList = swiperList; this.icons = icons; this.icon = icon; this.recommendList = recommendList; this.weekendList = weekendList; }); }, //如果页面设置了keep-alive缓存则每次进入页面都会触发的函数 activated()和deactivated() // (因为主页面设置了放置缓存不会发送请求(当city变化时要发送请求取主页面相应数据)) + activated() { if(this.currentCity!=this.city){ this.currentCity=this.city; homeModel.getlist(this.city).then(res => { var swiperList = res.data.gallary; var temp = res.data.icons; var recommendList = res.data.recommendList; var weekendList = res.data.weekendList; var icon = temp.slice(0, 8); var icons = temp.slice(8, 10); this.swiperList = swiperList; this.icons = icons; this.icon = icon; this.recommendList = recommendList; this.weekendList = weekendList; }); } }, //另一个方法使用监听属性监听值得变化(created函数中不需要this.currentCity=this.city) + // watch:{ // city(){ // homeModel.getlist(this.city).then(res => { // var swiperList = res.data.gallary; // var temp = res.data.icons; // var recommendList = res.data.recommendList; // var weekendList = res.data.weekendList; // var icon = temp.slice(0, 8); // var icons = temp.slice(8, 10); // this.swiperList = swiperList; // this.icons = icons; // this.icon = icon; // this.recommendList = recommendList; // this.weekendList = weekendList; // }); // } // }, ~~~