ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

vue tabBar导航栏设计实现3-进一步抽取tab-item

2022-01-29 16:36:04  阅读:221  来源: 互联网

标签:vue tab TabBarItem item tabBar 组件 defineComponent


系列导航

一、vue tabBar导航栏设计实现1-初步设计

二、vue tabBar导航栏设计实现2-抽取tab-bar

三、vue tabBar导航栏设计实现3-进一步抽取tab-item

四、vue tabBar导航栏设计实现4-再次抽取MainTabBar

五、vue tabBar导航栏设计实现5-最终版本

tabBar导航栏设计3-进一步抽取tab-item

一、本节目标效果

抽取一个tab-bar-item组件负责导航栏的布局

 

二、代码结构

 

 

 

注:主要是标红的几个文件

三、代码

重新编写这几个文件中的代码

App.vue

<template>
	<div id="app">
		<tab-bar>
			<tab-bar-item>
				<template v-slot:item-icon>
					<img :src="require('./assets/img/tabbar/home.svg')">
				</template>
				<template v-slot:item-text>
					 <div slot="item-text">首页</div>
				</template>
			</tab-bar-item>
			<tab-bar-item>
				<template v-slot:item-icon>
					 <img :src="require('./assets/img/tabbar/category.svg')">
				</template>
				<template v-slot:item-text>
					 <div slot="item-text">分类</div>
				</template>
			</tab-bar-item>
			<tab-bar-item>
				<template v-slot:item-icon>
					<img :src="require('./assets/img/tabbar/shopcart.svg')">
				</template>
				<template v-slot:item-text>
					<div slot="item-text">购物车</div>
				</template>
			</tab-bar-item>
			<tab-bar-item>
				<template v-slot:item-icon>
					 <img :src="require('./assets/img/tabbar/profile.svg')">
				</template>
				<template v-slot:item-text>
					 <div slot="item-text">我的</div>
				</template>
			</tab-bar-item>
		</tab-bar>
	</div>
</template>


<script>
	import {
		defineComponent
	} from 'vue'
	import TabBar from './components/tabbar/TabBar'
	import TabBarItem from './components/tabbar/TabBarItem'
	export default defineComponent({
		//组件名称
		name: 'App',
		//接收父组件的数据
		props: {},
		components: {
			TabBar,
			TabBarItem
		},
		setup(props, ctx) {
			return {}
		}
	})
</script>

<style lang="scss">
	@import "./assets/css/base.css";
</style>

 

TabBar.vue

<template>
  <div id="tab-bar">
	  <slot></slot>
  </div>  
</template>

<script>
	
import {defineComponent} from 'vue'
 
export default defineComponent({
    //组件名称
    name:'TabBar',
    //接收父组件的数据
    props:{
    },
	components: {
	  
	},
    setup(props,ctx){
        return{   
        }
    }    
})	
	
 
</script>

<style lang="scss">
  #tab-bar {
    display: flex;
    background-color: #f6f6f6;

    position: fixed;
    left: 0;
    right: 0;
    bottom: 0;

    box-shadow: 0 -1px 1px rgba(100,100,100,.2);
  }
  
  

</style>

 

TabBarItem.vue

<template>
  <div class="tab-bar-item">
    <slot name="item-icon"></slot>
    <slot name="item-text"></slot>
  </div>
</template>

<script>
 
  import {defineComponent} from 'vue'
   
  export default defineComponent({
      //组件名称
      name:'TabBarItem',
      //接收父组件的数据
      props:{
      },
  	components: {
  	  
  	},
      setup(props,ctx){
          return{   
          }
      }    
  })	
</script>

<style lang="scss">
  .tab-bar-item {
    flex: 1;
    text-align: center;
    height: 49px;
    font-size: 14px;
  }

  .tab-bar-item img {
    width: 24px;
    height: 24px;
    margin-top: 3px;
    vertical-align: middle;
    margin-bottom: 2px;
  }
</style>

 

base.css

body {
  padding: 0;
  margin: 0;
}

 

一些图片

四、代码按照步骤解释

1、抽取TabBarItem组件,利用插槽使该组件独立

 

 

 

 

 

2、数据放到App.vue  注:具体看源码 引入TabBar和TabBarItem两个组件

 

 

标签:vue,tab,TabBarItem,item,tabBar,组件,defineComponent
来源: https://www.cnblogs.com/yclh/p/15855307.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有