只需两步即可实现navbar封装,跟着我走就得了,cv你该会吧!
封装的custNavbar.vue组件
javascript
<template>
<div class="nav-all">
<van-nav-bar :title="props.title" left-arrow @click-left="goBack" class="navbar">
<template #right>
<van-icon :name="props.uname" size="18" />
</template>
</van-nav-bar>
</div>
</template>
<script setup>
import { ref } from 'vue';
// 引入路由模块
import { useRouter } from 'vue-router'
const router = useRouter()
const props = defineProps({
// 标题
title: {
type: String,
required: true
},
// 图标
uname: {
type: String,
required: true
}
});
const goBack = () => {
// window.history.back();
// 返回上一页
router.back();
};
</script>
// 样式配置,不遮挡数据显示
<style lang="scss" scoped>
.nav-all {
overflow: hidden;
margin-bottom: 45px;
.navbar {
position: fixed;
top: 0;
width: 100%;
background-color: #fff;
}
}
</style>
在使用的地方引入custNavbar.vue组件
javascript
<template>
<div class="artall">
// 引入的custNavbar组件
<custNavbar title="qwe" uname="search"></custNavbar>
// 模拟的数据
<p>qwe111111</p>
<p>qwe</p>
<p>qwe</p>
<p>qwe</p>
<p>qwe</p>
<p>qwe</p>
<p>qwe</p>
<p>qwe</p>
<p>qwe</p>
<p>qwe</p>
<p>qwe</p>
<p>qwe</p>
<p>qwe</p>
<p>qwe</p>
<p>qwe</p>
<p>qwe</p>
<p>qwe</p>
<p>qwe</p>
<p>qwe</p>
<p>qwe</p>
<p>qwe</p>
<p>qwe</p>
<p>qwe</p>
<p>qwe</p>
<p>qwe</p>
<p>qwe</p>
<p>qwe</p>
<p>qwe</p>
<p>qwe</p>
<p>qwe22222222222</p>
</div>
</template>
<script setup>
// 引入位置,根据自己的组件位置引入即可
import custNavbar from '@/components/custNavbar/index.vue'
</script>
<style lang="scss" scoped></style>
效果呈现: