制作微信小程序 第四天(组件)入门完结!

1.自定义组件流程:创建文件夹 直接右键新建
在这里插入图片描述
再找到要使用这个组件的文件的json,把相对路径写下
在这里插入图片描述
最后到wxml文件下使用就可以了(⚠️注意文件名tabs是组件的文件名称)
在这里插入图片描述2.tabs组件优化
.js

// companies/tabs/Tabs.js
Component({
  /**
   * 组件的属性列表
   */
  properties: {

  },

  /**
   * 组件的初始数据
   */
  data: {
    tabs:[
      {
        id:0,
        name:"首页",
        isActive:true
      },
      {
        id:1,
        name:"原创",
        isActive:false
      },
      {
        id:2,
        name:"分类",
        isActive:false
      },
      {
        id:3,
        name:"关于",
        isActive:false
      }
      

    ]
  },

  /**
   * 组件的方法列表
   */
  methods: {

  }
})

.wxml

<view class="tabs">
<view class="tabs_title">
  <!--<view class="title_item active">首页</view>
  <view class="title_item">原创</view>
  <view class="title_item">分类</view>
  <view class="title_item">关于</view>-->
  <view wx:for="{{tabs}}"
  wx:key="id"
  class="title_item {{item.isActive?'active':''}}"
  >
  {{item.name}}
  </view>
</view>
  <view class="tabs_content">内容</view>
</view>

wxss

.tabs{}
.tabs_title{
  display:flex;
  padding:10rpx 0;
}
.title_item{
  flex:1;
  display: flex;
  justify-content: center;
  align-items: center;
}
.active{
  color:red;
  border-bottom: 5rpx solid currentColor;
}
.tabs_content{}

在这里插入图片描述
3.标题选中
.js

// companies/tabs/Tabs.js
Component({
  /**
   * 组件的属性列表
   */
  properties: {

  },

  /**
   * 组件的初始数据
   */
  data: {
    tabs:[
      {
        id:0,
        name:"首页",
        isActive:true
      },
      {
        id:1,
        name:"原创",
        isActive:false
      },
      {
        id:2,
        name:"分类",
        isActive:false
      },
      {
        id:3,
        name:"关于",
        isActive:false
      }
      

    ]
  },

  /**
   * 组件的方法列表
   */
  methods: {
    hanldeItemTap(e){
      const {index}=e.currentTarget.dataset;
      let {tabs}=this.data;
      tabs.forEach((v,i)=>i===index?v.isActive=true:v.isActive=false);
      this.setData({
        tabs
      })
    }
  }
})

.wxml

.tabs{}
.tabs_title{
  display:flex;
  padding:10rpx 0;
}
.title_item{
  flex:1;
  display: flex;
  justify-content: center;
  align-items: center;
}
.active{
  color:red;
  border-bottom: 5rpx solid currentColor;
}
.tabs_content{}

.wxss

.tabs{}
.tabs_title{
  display:flex;
  padding:10rpx 0;
}
.title_item{
  flex:1;
  display: flex;
  justify-content: center;
  align-items: center;
}
.active{
  color:red;
  border-bottom: 5rpx solid currentColor;
}
.tabs_content{}

可以来回切换
在这里插入图片描述
4.父向子传递数据
在这里插入图片描述在这里插入图片描述
5.slot(点击文字有对应的页面)

在这里插入图片描述在这里插入图片描述在这里插入图片描述在这里插入图片描述
需要更多的学习,一定要看微信小程序的官网 好好研究!

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章