vue3 taro-ui-vue3 封装Tabbar组件


前言

提示:根据官网地址进行安装配置创建项目:

taro官网
taro-ui-vue3官网
演示
在这里插入图片描述
在这里插入图片描述


提示:taro-vue3 创建完项目后

一、taro-ui-vue3安装/引用

1.安装

在项目跟目录执行 npm install taro-ui-vue3 命令安装

2.引用

app.js 引用全局样式

// 引用全局样式
import 'taro-ui-vue3/dist/style/index.scss'

图片如下(示例):
在这里插入图片描述

二、配置Tabbar组件

1.创建组件

components 目录下创建 Tabbar.vue
图片如下(示例):
在这里插入图片描述
Tabbar.vue代码如下(示例):

<template>
  <view class="tabbar">
    <AtTabBar
      fixed
      :tabList="tabList"
      :current="current"
      @click="handleClick"
    />
  </view>
</template>

<script>
import {AtTabBar} from 'taro-ui-vue3'

export default {
  props: {
    tabKey: {
      type: [String, Number],
      default: 0,
    },
  },
  data() {
    return {
      current: 0,
      tabList: [
        {title: '首页', iconType: 'home', pagePath:'/pages/index/index'},
        {title: '博客', iconType: 'bullet-list',pagePath: "/pages/blog/index"},
        {title: '我的', iconType: 'user', pagePath: "/pages/my/index"}
      ],
    }
  },
  methods:{
    handleClick(value){
      let url = this.$data.tabList[value].pagePath
      wx.switchTab({
        url:url,
      })
    }
  },
  components: {
    AtTabBar,
  }
}
</script>

2.配置tabBar

app.config.js 配置配置

图片如下(示例):
在这里插入图片描述

app.config.js代码如下(示例):

export default defineAppConfig({
  pages: [
    'pages/index/index',
    'pages/my/index',
    'pages/blog/index',
  ],
  window: {
    backgroundTextStyle: 'light',
    navigationBarBackgroundColor: '#fff',
    navigationBarTitleText: 'WeChat',
    navigationBarTextStyle: 'black'
  },
  "tabBar": {
    "custom": true,
    "color": "#a9b7b7",
    "selectedColor": "#11cd6e",
    "borderStyle": "black",
    "list": [
      {
        "pagePath": 'pages/index/index',
        "text": "首页"
      },
      {
        "pagePath": 'pages/blog/index',
        "text": "博客"
      },
      {
        "pagePath": "pages/my/index",
        "text": "我的"
      }
    ]
  }
})

3.使用tabBar

pages 目录下创建 blog index my 文件

图片如下(示例):
在这里插入图片描述
index目录 index.vue 代码如下(示例):

<template>
  <view class="index">
    <text>{{ msg }}</text>
    <Tabbar></Tabbar>
  </view>
</template>

<script>
import {ref} from 'vue'
import './index.scss'
import Tabbar from "../../components/Tabbar"

export default {
  setup() {
    const msg = ref('Hello world index')
    return {
      msg
    }
  },
  components: {
    Tabbar,
  }
}

</script>

blog目录 index.vue 代码如下(示例):

<template>
  <view class="blog">
    <text>{{ msg }}</text>
    <Tabbar ref="tabbar"></Tabbar>
  </view>
</template>

<script>
import {ref} from 'vue'
import './index.scss'
import Tabbar from "../../components/Tabbar"

export default {
  setup() {
    const msg = ref('Hello world blog')
    return {
      msg
    }
  },
  onShow(){
    this.$refs.tabbar.current=1
  },
  components: {
    Tabbar,
  }
}

</script>

my目录 index.vue 代码如下(示例):

<template>
  <view class="my">
    <text>{{ msg }}</text>
    <Tabbar ref="tabbar"></Tabbar>
  </view>
</template>

<script>
import {ref} from 'vue'
import './index.scss'
import Tabbar from "../../components/Tabbar"

export default {
  setup() {
    const msg = ref('Hello world my')
    return {
      msg
    }
  },
  onShow(){
    this.$refs.tabbar.current=2
  },
  components: {
    Tabbar,
  }
}

</script>

我的小程序二维码

(https://img-blog.csdnimg.cn/6f0dac267ad741c99198a3e724ffdca8.png)

  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

荔枝学习

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

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

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

打赏作者

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

抵扣说明:

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

余额充值