需求:1. 从stockList页面到stockInfo页面,从stockInfo页面返回stockList,缓存stockList页面
           2. 从其他页面进入到stockList页面,则不缓存stockList页面


路由配置文件中:

{
  path: '/stockSelectionAndDiagnosticStocks',
  name: 'stockSelectionAndDiagnosticStocks',
  component: stockSelectionAndDiagnosticStocks,
  redirect: '/stockSelectionAndDiagnosticStocks/stockList',
  children: [
    {
      path: '/stockSelectionAndDiagnosticStocks/stockList',
      name: 'stockList',
      component: stockList,
      // 此处设置的原因:保证第一次从stockList进入到stockInfo中是缓存了的
      meta: { keepAlive: true }
    },
    {
      path: '/stockSelectionAndDiagnosticStocks/stockInfo',
      name: 'stockInfo',
      component: stockInfo
    }
  ]
}

App.vue中:

<div id="app">
  <keep-alive>
    <router-view v-if="$route.meta.keepAlive"></router-view>
  </keep-alive>
  <router-view v-if="!$route.meta.keepAlive"></router-view>
</div>

stockList.vue中:

beforeRouteLeave (to, from, next) {
  // 从列表页去到别的页面,如果不是详情页,则不缓存列表页
  if (to.name !== 'stockInfo') {
    this.$route.meta.keepAlive = false
  } else {
    this.$route.meta.keepAlive = true
  }
  next()
}

*** 增加需求:从stockInfo页面通过“特殊的”点击事件回到stockList,stockList页面的列表需要按照字段更新 ***

stockInfo.vue中:(其中setStore、getStore是封装的sessionStorage)

clickHistoryItem (val) {
  setStore('searchName', val)
  this.$router.push({ name: 'stockList' })
}

stockList.vue中:

// 只有当缓存的情况写才触发这两个钩子函数
activated () {
  if (getStore('searchName')) {
    this.pageIndex = 1
    this.searchStockFirstStep(getStore('searchName'))
  }
},
deactivated () {
  removeStore('searchName')
}

 

Logo

前往低代码交流专区

更多推荐