//在index.js中
{
path: '/SearchContent',
name: 'SearchContent',
component: SearchContent,
meta: {
keepAlive: false,//此组件不需要被缓存
isBack:false
}
},
{
path: '/Shop',
name: 'Shop',
component: Shop,
meta: {
keepAlive: false,
isBack:false//判断上一个页面
}
},
//在searchContent中
beforeRouteEnter(to, from, next) {
// 设置下一个路由的 meta
if(from.name == 'Shop'){
to.meta.isBack = true;
to.meta.keepAlive = true;
} // 让 A 缓存,即不刷新
next();
},
components:{
"app-brands":Brand
},
//注意activated只能在keep-alive组件中调用
activated() {
if(!this.$route.meta.isBack){
// 如果isBack是false,表明需要获取新数据,否则就不再请求,直接使用缓存的数据
this.getData();
this.getTree();
}
// 恢复成默认的false,避免isBack一直是true,导致下次无法获取数据
this.$route.meta.isBack=false
},
//在进入页面时调用接口请求数据,当数据请求完成,第二次进入页面时,就会执行activated函数
mounted(){
this.getData()
this.getTree()
},