vue项目列表跳转详情再返回列表还在原位置

news/2024/7/10 1:33:23 标签: vue

 keep-alive 是 Vue 内置的一个组件,可以使被包含的组件保留状态,或避免重新渲染 。也就是所谓的组件缓存

1.app.vue

<template>
  <div id="app">
    <!-- <router-view/> -->
    <!-- 可以被缓存的视图组件 -->
    <keep-alive>
      <router-view v-if="$route.meta.keepAlive"></router-view>
    </keep-alive>
    <!-- 不可以被缓存的视图组件 -->
    <router-view v-if="!$route.meta.keepAlive"></router-view>
  </div>
</template>

2、router/index.js

const router = new VueRouter({
    mode: 'history',
     routes: [
        {
            path: '/list',
            name: 'list',
            component: () =>
                import ('../views/list.vue'),
            meta: {
                keepAlive: true //此页面需要缓存
            }
        },
        {
            path: '/list/:id',
            name: 'detail',
            component: () =>
                import ('../views/detail.vue'),
            meta: {
                keepAlive: false //此页面不需要缓存
            }
        },
    ]
})

3、detail.vue

beforeRouteLeave(to, from, next) : 离开路由之前执行的函数,可用于页面的反向传值,页面跳转。

beforeRouteLeave(to,from,next){

    //设置下一个路由的meta,让列表页面缓存,即不刷新

    to.meta.keepAlive = true

    next()

},

 


http://www.niftyadmin.cn/n/1866787.html

相关文章

vue项目中使用可编辑div contenteditable

<div ref"InputContainer" contenteditable"true" spellcheck"false" placeholder"文字"></div> spellcheck 属性规定是否对元素进行拼写和语法检查 css设置placeholder文字的颜色 *[contenteditable"true"]:…

vue的H5项目中使用@scroll滚动加载数据

<div ref"list" scroll"handleScroll"></div> created() created() { //判断是手机端和pc端 this.device() if(this.isPc true){ this.clientHeight document.documentElement.clientHeight; window.addEventListener(scroll, this.handleSc…

vue 点击按钮div input focus问题解决

<div click"clickFocus"></div> <input ref"inputbox" v-model"inputText" type"text" > methods: { clickFocus(){ this.$nextTick(() > this.$refs.inputbox.focus()); }, }

uni-app开发安卓app的封装get和post及页面使用

request.js页面如下 const baseUrl = "请求地址" //get请求 //get请求封装 export function getRequest(url,params){return new Promise((resolve, reject) => {uni.request({url: baseUrl + url,data: params,method:"GET",dataType:json,header:{…

uni-app自定义底部tabbar实现某个tab的显示和隐藏

自定义的tabbar可用v-if实现某个tab的显示和隐藏 页面 <view class"tab-box row acenter"><navigator class"grow1 column acenter jcenter" hover-class"none" url"" open-type"switchTab"><image class&…

uni-app修改swiper里dot的样式

注意&#xff1a;必须写在App.vue页面 <style>/* 首页的banner dot效果 */.home uni-swiper .uni-swiper-dot {width:6rpx;height:6rpx;}.home uni-swiper .uni-swiper-dot-active {width: 34rpx;border-radius: 20rpx;} </style>

uni-app修改checkbox样式

注意&#xff1a;必须写在App.vue页面 <style>uni-checkbox .uni-checkbox-input {border-radius: 50% !important;color: #ffffff !important; }uni-checkbox .uni-checkbox-input.uni-checkbox-input-checked {color: #fff;background: #1953ed;} </style>

uni-app swiper实现通知上下循环滚动

swiper实现通知上下循环滚动 <template> <view><swiper circular"true" vertical"true" autoplay"true" interval"3000" duration"1000" ><swiper-item v-for"(item,index) in noticeList"…