美文网首页
vue的页面缓存,菜单点击刷新页面

vue的页面缓存,菜单点击刷新页面

作者: JennyGao66 | 来源:发表于2018-06-28 16:13 被阅读0次

(1)列表页,1,2,3,4,5,6
切换到第3页,然后点击列表中的某一个查看详情,,再点击返回的时候,列表页切换到了第一页
(2)列表页做一系列的条件筛选后100条只剩下10条,然后点击10条中的一条去查看详情,再点击返回,列表重新回到100条
页面AppMain.vue:

<template>
  <section class="app-main">
    <transition name="fade" mode="out-in">
      <!-- <router-view :key="key"></router-view> -->
      <router-view></router-view>
    </transition>
  </section>
</template>

<script>
export default {
  name: 'AppMain',
  computed: {
    // key() {
    //   return this.$route.name !== undefined ? this.$route.name + +new Date() : this.$route + +new Date()
    // }
  }
}
</script>

<style rel="stylesheet/scss" lang="scss" scoped>

</style>

所以需要做缓存,在AppMain.vue页面

<keep-alive>
     <router-view></router-view>
</keep-alive>

达到上述需求,但是列表页转详情的时候,通过id获取的详情页面接口没有更新,没有重新获取接口,也就是说详情页也缓存了

所以又找了方法,修改AppMain.vue

<template>
  <section class="app-main">
  <transition name="fade" mode="out-in">
      <div>
        <keep-alive>
          <router-view v-if="$route.meta.keepAlive"></router-view>
        </keep-alive>
        <router-view v-if="!$route.meta.keepAlive"></router-view>
      </div>
    </transition>
  </section>
</template>

<script>
export default {
  name: 'AppMain',
  computed: {
    // key() {
    //   return this.$route.name !== undefined ? this.$route.name + +new Date() : this.$route + +new Date()
    // }
  }
}
</script>

<style rel="stylesheet/scss" lang="scss" scoped>

</style>

路由配置文件也增加:keepAlive: true 为

{
    path: 'channelPriceRecord',
    name: '渠道价格配置记录',
    component: () =>
                    import('@/views/params/channelPriceRecord'),
    meta: { title: '渠道价格配置记录', icon: 'tree', keepAlive: true }
  }

菜单点击刷新页面

通过改变router-view中的key来达到刷新组件的目的

参看链接https://blog.csdn.net/mnhn456/article/details/78758789/

<template>
  <section class="app-main">
  <transition name="fade" mode="out-in">
      <div>
        <keep-alive>
          <router-view v-if="$route.meta.keepAlive" :key='key'></router-view>
        </keep-alive>
        <router-view v-if="!$route.meta.keepAlive" :key='key'></router-view>
      </div>
    </transition>
  </section>
</template>

<script>
export default {
  name: 'AppMain',
  computed: {
    key() {
      return this.$route.name !== undefined ? this.$route.name + +new Date() : this.$route + +new Date()
    }
  }
}
</script>

<style rel="stylesheet/scss" lang="scss" scoped>

</style>

相关文章

  • vue的页面缓存,菜单点击刷新页面

    (1)列表页,1,2,3,4,5,6切换到第3页,然后点击列表中的某一个查看详情,,再点击返回的时候,列表页切换到...

  • Vue错误集

    vue-router使用hash模式时,页面点击跳转或者刷新正常.使用history模式时,页面点击跳转正常,刷新...

  • UICollectionView的隐式动画消除

    背景 页面使用了UICollectionView,有要求点击底部菜单选项的时候需要再次刷新页面内容。在刷新的时候会...

  • 100、ant-design-vue 刷新页面导航栏自动定位到首

    问题:在vue项目中使用了vue-router,页面刷新、浏览器前进后退都会导致导航菜单首页高亮显示 在导航栏点击...

  • 一文搞定为什么nginx部署页面刷新404

    我们在用nginx部署时候完项目会出现页面刷新会404,而页面上点击跳转(一般比如点击菜单)页面则不会404,为什...

  • vue 路由跳转&部分页面缓存&页面动画切换

    前言:此篇仅做记录。 上一下原先的代码,实现路由跳转&页面动画切换,没做页面缓存,每个页面都要刷新(App.vue...

  • vue keep-alive缓存数据方法+tips

    主要针对页面点击返回时不重新加载页面,直接获取缓存。 APP.vue 页面 当路由的meta属性的keepAliv...

  • keepAlive使用方法

    vue单页面,多路由,前进刷新,后退不刷新 目的:vue-cli构建的vue单页面应用,某些特定的页面,实现前进刷...

  • vue-router用法

    vue 路由 路由点击跳转不会页面刷新,可以保留数据 安装vue-router:npm install vue-r...

  • 刷新当前路由

    有时候我们点击菜单时想要刷新该菜单的页面内容,这个时候直接导航到当前页面本身会报错,我们可以在导航前检查是否导航到...

网友评论

      本文标题:vue的页面缓存,菜单点击刷新页面

      本文链接:https://www.haomeiwen.com/subject/jtagyftx.html