插槽快速实现vue项目tabbar导航

在vue项目中,快速用插槽实现tabbar导航,只要在页面在中写具体业务逻辑,实现敏捷开发。
这是TabBar的组件代码:

<template>
  <div id="tab-bar">
    <!-- 用插槽实现tabbar -->
    <slot></slot>
  </div>
</template>
<script>
export default {
  name: "TabBar"
};
</script>

<style  scoped>
#tab-bar {
  display: flex;
  background: #f6f6f6;
  position: fixed;
  left: 0;
  right: 0;
  bottom: 0;
  box-shadow: 0 -1px 1px rgba(100, 100, 100, 0.2);
  justify-content: space-around;
}
</style>

这是TabBarItem的组件代码:

<template>
  <div>
    <div class="tab-bar-item" @click="tabClick">
      <!-- 用插槽是实现tabitem -->
      <div v-if="!isActive">
        <slot name="item-icon"></slot>
      </div>
      <div v-else>
        <slot name="item-icon-active"></slot>
      </div>
      <div :style="activeStyle">
        <slot name="item-text"></slot>
      </div>
    </div>
  </div>
</template>
<script>
export default {
  name: "TabBarItem",
  props: {
    path: String,
    activeColor: {
      type: String,
      default: "gray"
    }
  },
  data() {
    return {
      // isActive: true
    };
  },
  computed: {
    isActive() {
      return this.$route.path.indexOf(this.path) !== -1;
    },
    activeStyle() {
      return this.isActive ? { color: this.activeColor } : {};
    }
  },
  methods: {
    tabClick() {
      console.log("methods");
      this.$router.replace(this.path);
    }
  }
};
</script>
<style  scoped>
.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: 3px;
}
</style>

只需要在app.vue配置你需要的几个导航,例子:

<template>
  <div id="app">
    <tab-bar>
        <!-- 这里只要增加tabbar就行,组件内部不需要修改 -->
      <tab-bar-item path="/home" activeColor="red">
        <img slot="item-icon" src="./assets/img/tabbar/main.png" alt="" />
        <img slot="item-icon-active" src="./assets/img/tabbar/selectedmain.png" alt="">
        <div slot="item-text">首页</div>
      </tab-bar-item>
      <tab-bar-item path="/message" activeColor="red">
        <img slot="item-icon" src="./assets/img/tabbar/message.png" alt="" />
         <img slot="item-icon-active" src="./assets/img/tabbar/selectedmessage.png" alt="">
        <div slot="item-text">消息</div>
      </tab-bar-item>
      <tab-bar-item path="/mine" activeColor="red">
        <img slot="item-icon" src="./assets/img/tabbar/mine.png" alt="" />
         <img slot="item-icon-active" src="./assets/img/tabbar/selectedmine.png" alt="">
        <div slot="item-text">我的</div>
      </tab-bar-item>
    </tab-bar>
    <router-view></router-view>
  </div>
</template>

<script>
/* 引入组件 */
import TabBar from "./components/tabbar/TabBar";
import TabBarItem from "./components/tabbar/TabBarItem";
export default {
  name: "App",
  components: {
    TabBar,
    TabBarItem
  }
};
</script>

<style>
/* 在style中引入样式方法 */
@import "./assets/css/base.css";
</style>

activeColor=“red” 传入选中的颜色,颜色根据自己需求填, TabBar组件和TabBarItem都不需要改动,就能快速实现底部导航。
我把完整的代码附上。

https://download.csdn.net/download/qq_14948637/12155611
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

lihaiyuancoder

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值