使用keep-alive优化网页性能

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

最近项目,欢迎start

使用keep-alive优化网页性能


使用keep-alive优化网页性能


使用keep-alive优化网页性能
在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>