vue自定义组件实现底部导航栏动态切换,前端开发工程学习

routes: [

{

path: ‘/member’,

name: ‘member’,

component: member,

},

{

path: ‘/’,

name: ‘首页’,

redirect: ‘/member’,

component: member,

},

{

path: ‘/car’,

name: ‘car’,

component: car,

},

{

path: ‘/my’,

name: ‘my’,

component: my,

},

],

mode: “history”//干掉地址栏里边的#号键

});

export default router

三、开始创建子组件(components文件下新建 footer.vue),注意:底部导航图片需要通过require方式引入,子组件通过props方法来接收父组件传来的值:footIndex1

v-for=“(item,index) in footerImg”

@click=“clickFoo(index)”

:key=“index”

:id=“index”

:class=“index==timepartValue?‘commColor’:‘’”

{{item.name}}

四、在父组件app.vue中引用刚才创建的组件:

import footercon from “@/components/footer”;

注册组件:

components: {

footercon

},

通过v-show="footer_show"来控制底部导航的显示隐藏

此时需要在新建的三个底部导航页面中通过$emit来给父组件app.vue中传值来实现底部显示隐藏:

export default {

data(){

return{}

},

created() {

this.$emit(“footer”, true);

this.$emit(“checkHomeIndex”, 0);

}

this.$emit(“checkHomeIndex”, 0);//用来传当前页面的下标,用来动态切换导航

this.$emit(“footer”, true);//表示此时底部导航显示,false为隐藏

app.vue完整代码:

  • 6
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Vue3中可以通过自定义组件实现底部导航栏的灵活组件。以下是一个简单的实现示例: 1. 创建一个TabBar组件,用于展示底部导航栏: ```vue <template> <div class="tab-bar"> <slot></slot> </div> </template> <script> export default { name: "TabBar", }; </script> <style scoped> .tab-bar { display: flex; justify-content: space-around; align-items: center; position: fixed; bottom: 0; left: 0; right: 0; height: 50px; background-color: #fff; box-shadow: 0 -3px 5px rgba(0, 0, 0, 0.1); } </style> ``` 2. 在TabBar组件中使用slot插槽,用于接收底部导航栏的子组件。例如: ```vue <template> <div class="tab-bar"> <slot></slot> </div> </template> <script> export default { name: "TabBar", }; </script> <style scoped> .tab-bar { display: flex; justify-content: space-around; align-items: center; position: fixed; bottom: 0; left: 0; right: 0; height: 50px; background-color: #fff; box-shadow: 0 -3px 5px rgba(0, 0, 0, 0.1); } </style> ``` 3. 创建底部导航栏子组件,例如TabBarItem。在TabBarItem中可以定义图标、文字和点击事件等属性。例如: ```vue <template> <div class="tab-bar-item" :class="{ active: active }" @click="handleClick"> <i :class="icon"></i> <span>{{ title }}</span> </div> </template> <script> export default { name: "TabBarItem", props: { icon: { type: String, required: true, }, title: { type: String, required: true, }, active: { type: Boolean, required: true, }, }, methods: { handleClick() { this.$emit("click"); }, }, }; </script> <style scoped> .tab-bar-item { display: flex; flex-direction: column; align-items: center; font-size: 12px; color: #666; } .tab-bar-item i { font-size: 20px; margin-bottom: 2px; } .tab-bar-item.active { color: #007aff; } </style> ``` 4. 在父组件中使用TabBar和TabBarItem组件。例如: ```vue <template> <div> <router-view /> <TabBar> <TabBarItem v-for="(tab, index) in tabs" :key="index" :icon="tab.icon" :title="tab.title" :active="index === activeIndex" @click="activeIndex = index" /> </TabBar> </div> </template> <script> import TabBar from "@/components/TabBar.vue"; import TabBarItem from "@/components/TabBarItem.vue"; export default { name: "App", components: { TabBar, TabBarItem, }, data() { return { tabs: [ { icon: "icon-home", title: "首页", }, { icon: "icon-category", title: "分类", }, { icon: "icon-cart", title: "购物车", }, { icon: "icon-person", title: "个人中心", }, ], activeIndex: 0, }; }, }; </script> ``` 在父组件中使用TabBar和TabBarItem组件,通过循环遍历tabs数组,动态生成底部导航栏子组件TabBarItem,并通过activeIndex属性控制当前激活的子组件。当子组件被点击时,通过click事件向父组件发送消息,触发activeIndex的变化,从而实现底部导航栏切换

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值