vue3 h5自定义tabbar并用keep-alive保存缓存路由

        

  1. 路由嵌套
  2. 封装tabbar组件
  3. 创建一个容器放tabbar和子路由
  4. keep-alive保存路由状态

1.路由嵌套

{
		path: '/',
		name: 'index',
		component: () => import('@/views/index.vue'),
		children:[
			{
				path: '',
				redirect:'/com',
				meta:{
					keepAlive: true
				}
			},
			{
				path: '/com',
				name: 'com',
				component: () => import('@/views/com.vue'),
				meta:{
					keepAlive: true
				}
			},
			{
				path: '/info',
				name: 'info',
				component: () => import('@/views/info.vue'),
				meta:{
					keepAlive: true
				}
			},
			{
				path: '/mine',
				name: 'mine',
				component: () => import('@/views/mine.vue'),
				meta:{
					keepAlive: true
				},
			},
		]
	},

2.封装tabbar组件

src/components/tabbar

<template>
	<div class='tabbarBox'>
		<div class="tabbar-box" v-if="tabs.length > 0">
			<div class="tabbar-item" v-for="(item, index) in tabs" :key="index"
				:class="{ 'active': activeIndex === index }" @click="selectTab(index,item.path)">
				{{ item.title }}
			</div>
		</div>
	</div>
</template>
<script setup lang="ts">

// 接收参数
const props = defineProps({
	tabs: {
		type: Array,
		default: () => { },
	},
});

const router = useRouter()
const tabs = ref<any>([])
const activeIndex = ref<number>(0);

watchEffect(() => {
	tabs.value = props.tabs;
});

const selectTab = (index:number,path:string) => {
  activeIndex.value = index;
		router.push(path)
};
 
onMounted(() => {
})
</script>

3.创建一个容器放tabbar和子路由

src/view/index.vue

网上都是把tabbar放在app.vue里边的,但是放在app.vue每个页面都会显示了还需要判断,直接弄一个容器专门放tabbar页面

<template>
  <div class="tabbar">
    <tabbar :tabs='tabs'/> 
    <router-view v-slot="{ Component }">
      <keep-alive>
        <component :is="Component" />
      </keep-alive>
    </router-view>
  </div>
</template>
<script setup lang="ts">
interface Tab {
  title: string;
  path: string;
}

const tabs: Tab[] = [
  { title: '社区', path: '/com' },
  { title: '消息', path: '/info' },
  { title: '我的', path: '/mine' },
];

onMounted(() => {

})
</script>
<style scoped lang='less'>
.tabbar {
  width: 100vw;
  height: 100vh;
  background: #F7F6FB;
}
</style>

4.使用keep-alive

src/view/com.vue

<template>
	<div class='com-page' ref="comPage">
		<div class="com-box">社区</div>
	</div>
</template>
<script setup lang="ts">
import { useTemplateRef } from 'vue';

const scrollTop = ref<number>(0);
const compage = useTemplateRef<HTMLElement>("comPage");


onActivated(() => {
	nextTick(() => {
		// 跳转后��复 scrollTop 值,在下次路由跳转时��复 scrollTop 值
		if (compage.value) {
			compage.value.scrollTop = scrollTop.value
		}
	})
})
onBeforeRouteLeave((to, from, next) => {
	// 路由跳转前记录 scrollTop 值,在下次路由跳转时��复 scrollTop 值
	if (compage.value) {
		scrollTop.value = compage.value.scrollTop
	}
	next()
})
onMounted(() => {

})
</script>
<style scoped lang='less'>
.com-page {
	width: 100vw;
	height: 100vh;
	background: #F7F6FB;
	overflow: auto;
	font-size: 80px;

	.com-box {
		height: 8000px;
	}

}
</style>

useTemplateRef是vue3.5新发布的特性,用来获取dom

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值