小程序项目搭建

微信小程序项目搭建(Vant Weapp)

1.引入vant组件库

  1. npm init -y
  2. npm i
  3. 后续看vant官网流程即可

注意:如果步骤没错,且重复试了很多遍。引用vant组件却发现组件不带样式,这种情况看看开发者工具是否是最新的稳定版,不是,请更新。

2.自定义tabBar

  1. 把tabBar切换的页面创建好,然后在app.json
"tabBar": {
    "custom": true,
    "list": [
      {
        "text": "首页",
        "pagePath": "pages/index/index"
      },
      {
        "text": "分类",
        "pagePath": "pages/classify/classify"
      },
      {
        "text": "购物车",
        "pagePath": "pages/car/car"
      },
      {
        "text": "我的",
        "pagePath": "pages/my/my"
      }
    ]
  },
  1. 根目录下新建custom-tab-bar文件夹,然后新建index组件。icon文件夹存放的是自定义切换标签。

在这里插入图片描述

  1. index.js文件
// custom-tab-bar/index.js
Component({
  data: {
    active: 0,
    list: [
      {
        text:"首页",
        normal:"./icon/home.png",
        active:"./icon/home-active.png",
        url:"/pages/index/index"
      },
      {
        text:"分类",
        normal:"./icon/classify.png",
        active:"./icon/classify-active.png",
        url:"/pages/classify/classify"
      },
      {
        text:"购物车",
        normal:"./icon/car.png",
        active:"./icon/car-active.png",
        url:"/pages/car/car"
      },
      {
        text:"我的",
        normal:"./icon/my.png",
        active:"./icon/my-active.png",
        url:"/pages/my/my"
      },
    ]
  },
  methods: {
    onChange(e) {
      // e.detail 的值为当前选中项的索引
      wx.switchTab({url: this.data.list[e.detail].url})
    },
    init() {
      const page = getCurrentPages().pop();
      // 如果数组中某个子项的 url 等于 当前页面的路径 就返回这个子项的索引值
      this.setData({
        active: this.data.list.findIndex(item => item.url === `/${page.route}`)
      });
     }
  }
})

  1. index.wxml文件
<van-tabbar 
  active="{{ active }}" 
  inactive-color="#bfbfbf"
  active-color="#8f1a22"
  bind:change="onChange">
  <van-tabbar-item wx:for="{{ list }}" wx:key="index">
    <image 
      slot="icon"
      src="{{ item.normal }}"
      mode="aspectFit"
      style="width: 30px; height: 18px;"
    />
    <image
      slot="icon-active"
      src="{{ item.active }}"
      mode="aspectFit"
      style="width: 30px; height: 18px;"
    />
    {{ item.text }}
  </van-tabbar-item>
</van-tabbar>

  1. 最后在每个tab页面js中
/**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.getTabBar().init()
  },

注意:如果步骤全部正确,tabBar不显示出来,看看app.json里,有一个属性,把它删除就好

"lazyCodeLoading": "requiredComponents"
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值