使用keep-alive优化网页性能

该笔记为最近项目中的一点经验积累

最近项目,欢迎start

keep-alive:主要用于保留组件状态或避免重新渲染。

比如: 有一个列表页面和一个 详情页面,那么用户就会经常执行打开详情=>返回列表=>
打开详情,这样的话 列表 和 详情 都是一个频率很高的页面,那么就可以对列表组件使用<keep-alive></keep-alive>进行缓存,这样用户每次返回列表的时候,都能从缓存中快速渲染,而不是重新渲染。

当组件在<keep-alive> 内被切换,在 2.2.0 及其更高版本中,activated 和 deactivated生命周期 将会在 树内的所有嵌套组件中触发。
在这里插入图片描述


在这里插入图片描述


在这里插入图片描述
在Home.vue和City.vue中的都发送了ajax请求,所以每次页面刷新渲染的时候都会去发送ajax请求,这样无疑增加了网络负担。
因此,通过vue提供的keep-alive来将其保留在内存中来得以实现。

部分代码:

<template>
  <div id="app">
    <keep-alive>
      <router-view/>
    </keep-alive>
  </div>
</template>

<script>
export default {
  name: 'App'
}
</script>

<style lang="stylus" scoped>

</style>

但是,这样又会出一个问题,当我选中好一个城市的时候,返回首页的内容应该是随之变化的,而如果使用了keep-alive的话是直接从缓存中拿取数据了,这样又不能达到我们的目的,那么如何实现呢?
部分代码

<template>
  <div>
    <home-header></home-header>
    <home-swiper :list="swiperList"></home-swiper>
    <home-icons :list="iconList"></home-icons>
    <home-recommend :list="recommendList"></home-recommend>
    <home-weekends :list="weekendsList"></home-weekends>
  </div>
</template>

<script>
import HomeHeader from './components/HomeHeader'
import HomeSwiper from './components/Swiper'
import HomeIcons from './components/Icons'
import HomeRecommend from './components/Recommend'
import HomeWeekends from './components/Weekends'
import axios from 'axios'
import {mapState} from 'vuex'

export default {
  name: 'Home',
  data () {
    return {
      city: '',
      swiperList: [],
      iconList: [],
      recommendList: [],
      weekendsList: []
    }
  },
  components: {
    HomeWeekends,
    HomeSwiper,
    HomeHeader,
    HomeIcons,
    HomeRecommend
  },
  methods: {
    getHomeInfo () {
      // axios.get()返回的是一个promise对象
      axios.get('/api/index.json?city=' + this.city)
        .then(this.getHomeInfoSucc)
    },
    getHomeInfoSucc (res) {
      console.log(res)
      res = res.data
      if (res.ret && res.data) {
        const data = res.data
        this.city = data.city
        this.swiperList = data.swiperList
        this.iconList = data.iconList
        this.recommendList = data.recommendList
        this.weekendsList = data.weekendsList
      }
    }
  },
  computed: {
    ...mapState(['city'])
  },
  mounted () {
    this.getHomeInfo()
  }
}
</script>

<style scoped>

</style>

即在发送ajax的时候添加了参数:axios.get('/api/index.json?city=' + this.city)

当使用keep-alive组件的时候会多出一个Vue的生命周期函数activated

性能优化代码实现:

<template>
  <div>
    <home-header></home-header>
    <home-swiper :list="swiperList"></home-swiper>
    <home-icons :list="iconList"></home-icons>
    <home-recommend :list="recommendList"></home-recommend>
    <home-weekends :list="weekendsList"></home-weekends>
  </div>
</template>

<script>
import HomeHeader from './components/HomeHeader'
import HomeSwiper from './components/Swiper'
import HomeIcons from './components/Icons'
import HomeRecommend from './components/Recommend'
import HomeWeekends from './components/Weekends'
import axios from 'axios'
import {mapState} from 'vuex'

export default {
  name: 'Home',
  data () {
    return {
      lastCity: '',
      // city: '',
      swiperList: [],
      iconList: [],
      recommendList: [],
      weekendsList: []
    }
  },
  components: {
    HomeWeekends,
    HomeSwiper,
    HomeHeader,
    HomeIcons,
    HomeRecommend
  },
  methods: {
    getHomeInfo () {
      // axios.get()返回的是一个promise对象
      axios.get('/api/index.json?city=' + this.city)
        .then(this.getHomeInfoSucc)
    },
    getHomeInfoSucc (res) {
      console.log(res)
      res = res.data
      if (res.ret && res.data) {
        const data = res.data
        // this.city = data.city
        this.swiperList = data.swiperList
        this.iconList = data.iconList
        this.recommendList = data.recommendList
        this.weekendsList = data.weekendsList
      }
    }
  },
  computed: {
    ...mapState(['city'])
  },
  mounted () {
    this.lastCity = this.city
    console.log('mounted')
    this.getHomeInfo()
  },
  activated () {
    console.log('activated')
    if (this.lastCity !== this.city) {
      this.lastCity = this.city
      this.getHomeInfo()
    }
  }
}
</script>

<style scoped>

</style>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
Vue组件 keep-alive 和 transition 是Vue.js的两个核心特性。在实际开发中,它们经常被用于优化Vue应用程序的性能和用户体验。 Vue组件 keep-alive 可以缓存组件的状态和DOM结构,从而在组件重新渲染时不需要再重新创建DOM,可以大大提高应用的渲染效率。在使用keep-alive时,需要将要缓存的组件包裹在keep-alive标签内,并指定唯一的key值,这个key值可以用来进行缓存的管理。同时,可以通过activated和deactivated钩子函数来控制缓存的生命周期,比如在组件被激活时可以执行某些逻辑操作。但需要注意的是,由于缓存了组件的状态和DOM结构,因此对于一些需要实时数据的组件可能不适合使用keep-alive。 Vue组件 transition 则是用来实现过渡动画的组件。在使用transition时,需要指定过渡动画的类名,再将要过渡的DOM元素包裹在transition标签内即可。常见的过渡动画有常见的fade、slide等效果。同时,还可以通过各种hook函数来控制过渡动画的各个阶段,包括动画开始前、进行中和结束后的操作。需要注意的是,由于过渡动画需要不断地修改DOM结构,因此会带来一定的性能损耗,特别是在频繁进行过渡动画时。 综上所述,Vue组件 keep-alive 和 transition 都是非常重要的Vue.js特性,在实际开发中需要根据具体情况灵活应用,以达到优化应用性能的目的。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

. . . . .

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值