【微信小程序】6天精准入门(第4天:自定义组件及案例界面)附源码

这篇具有很好参考价值的文章主要介绍了【微信小程序】6天精准入门(第4天:自定义组件及案例界面)附源码。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

一、自定义组件

1、介绍

        从小程序基础库版本 1.6.3 开始,小程序支持简洁的组件化编程。所有自定义组件相关特性都需要基础库版本 1.6.3 或更高。

        开发者可以将页面内的功能模块抽象成自定义组件,以便在不同的页面中重复使用;也可以将复杂的页面拆分成多个低耦合的模块,有助于代码维护。自定义组件在使用时与基础组件非常相似

2、创建自定义组件

类似于页面,一个自定义组件由 json wxml wxss js 4个文件组成。要编写一个自定义组件,首先需要在 json 文件中进行自定义组件声明(将 component 字段设为 true 可将这一组文件设为自定义组件):

{
  "component": true
}

同时,还要在 wxml 文件中编写组件模板,在 wxss 文件中加入组件样式,它们的写法与页面的写法类似。具体细节和注意事项参见 组件模板和样式 。

在项目中创建一个components/tabs文件夹,新建Component文件

【微信小程序】6天精准入门(第4天:自定义组件及案例界面)附源码,# 微信小程序,微信小程序,小程序

【注意】

在新的版本里面我们会出现报错,但是在win7的一些旧版本里面是不会出现这些问题的,我们需要在project.config.json文件里面添加以下代码:

"ignoreDevUnusedFiles": false,
"ignoreUploadUnusedFiles": false,

【微信小程序】6天精准入门(第4天:自定义组件及案例界面)附源码,# 微信小程序,微信小程序,小程序

同时,还要在 wxml 文件中编写组件模板,在 wxss 文件中加入组件样式,它们的写法与页面的写法类似。具体细节和注意事项参见 组件模板和样式 。

代码示例:

<view class="inner">
  {{innerText}}
</view>
<slot></slot>
/* 这里的样式只应用于这个自定义组件 */
.inner {
  color: red;
}

注意:在组件wxss中不应使用ID选择器、属性选择器和标签名选择器。

在自定义组件的 js 文件中,需要使用 Component() 来注册组件,并提供组件的属性定义、内部数据和自定义方法。

组件的属性值和内部数据将被用于组件 wxml 的渲染,其中,属性值是可由组件外部传入的。更多细节参见 Component构造器 。

代码示例:

Component({
  properties: {
    // 这里定义了innerText属性,属性值可以在组件使用时指定
    innerText: {
      type: String,
      value: 'default value',
    }
  },
  data: {
    // 这里是一些组件内部数据
    someData: {}
  },
  methods: {
    // 这里是一个自定义方法
    customMethod: function(){}
  }
})

3、使用自定义组件

使用已注册的自定义组件前,首先要在页面的 json 文件中进行引用声明。此时需要提供每个自定义组件的标签名和对应的自定义组件文件路径:

{
  "usingComponents": {
    "tabs": "/components/tabs/tabs"
  }
}

这样,在页面的 wxml 中就可以像使用基础组件一样使用自定义组件。节点名即自定义组件的标签名,节点属性即传递给组件的属性值。

在wxml里面使用<tabs>自定义标签

<tabs inner-text='6666'></tabs>

4、案例---头部导航

使用自定义组件,实现头部导航的一个效果

【微信小程序】6天精准入门(第4天:自定义组件及案例界面)附源码,# 微信小程序,微信小程序,小程序

首先我们要在组件里面定义好

tabs.wxml

<view class="tabs">
    <view class="tabs_title">
        <view wx:for="{{tabList}}" wx:key="id" class="title_item  {{index==tabIndex?'item_active':''}}" bindtap="handleItemTap" data-index="{{index}}">
            <view style="margin-bottom:5rpx">{{item}}</view>
            <view style="width:30px" class="{{index==tabIndex?'item_active1':''}}"></view>
        </view>
    </view>
    <view class="tabs_content">
        <slot></slot>
    </view>
</view>

tabs.js

// components/tabs/tabs.js
Component({
    /**
     * 组件的属性列表
     */
    properties: {//定义了组件所需要的属性值
        tabList:Object
    },

    /**
     * 组件的初始数据
     */
    data: {

    },

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

wxss

/* components/tabs/tabs.wxss */
.inner {
    color: red;
  }

  .tabs {
    position: fixed;
    top: 0;
    width: 100%;
    background-color: #fff;
    z-index: 99;
    border-bottom: 1px solid #efefef;
    padding-bottom: 20rpx;
}

.tabs_title {
    /* width: 400rpx; */
    width: 90%;
    display: flex;
    font-size: 9pt;
    padding: 0 20rpx;
}

.title_item {
    color: #999;
    padding: 15rpx 0;
    display: flex;
    flex: 1;
    flex-flow: column nowrap;
    justify-content: center;
    align-items: center;
}

.item_active {
    /* color:#ED8137; */
    color: #000000;
    font-size: 11pt;
    font-weight: 800;
}

.item_active1 {
    /* color:#ED8137; */
    color: #000000;
    font-size: 11pt;
    font-weight: 800;
    border-bottom: 6rpx solid #333;
    border-radius: 2px;
}

我们在指定的页面调用自定义好的组件

json文件里面设置调用组件

{
    "usingComponents": {
       "tabs" : "/components/tabs/tabs"
    }
}

wxml文件里面添加自定义的组件

<tabs tabList="{{tabs}}" bindtabsItemChange="tabsItemChange">
</tabs>

在js文件里面设置值

// pages/meeting/list/list.js
Page({

    /**
     * 页面的初始数据
     */
    data: {
        tabs: ['会议中', '已完成', '已取消', '全部会议']
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {

    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    }
})

【微信小程序】6天精准入门(第4天:自定义组件及案例界面)附源码,# 微信小程序,微信小程序,小程序

二、案例界面设置

完成会议、投票、个人中心页面的设计

【微信小程序】6天精准入门(第4天:自定义组件及案例界面)附源码,# 微信小程序,微信小程序,小程序

1、会议

在上面已经拥有的基础上进行一个点击数据的展示

在自定义组件的js文件里面进行方法的编写

    /**
     * 组件的方法列表
     */
    methods: {
        handleItemTap(e){
            // 获取索引
            const {index} = e.currentTarget.dataset;
            // 触发 父组件的事件
            this.triggerEvent("tabsItemChange",{index})
            this.setData({
                tabIndex:index
            })
          }
    }

在会议的页面的wxml文件里面进行页面的编写

<tabs tabList="{{tabs}}" bindtabsItemChange="tabsItemChange">
</tabs>
<view style="height: 100rpx;"></view>
<block wx:for-items="{{lists}}" wx:for-item="item" wx:key="item.id">
    <view class="list" data-id="{{item.id}}">
        <view class="list-img al-center">
            <image class="video-img" mode="scaleToFill" src="{{item.image}}"></image>
        </view>
        <view class="list-detail">
            <view class="list-title"><text>{{item.title}}</text></view>
            <view class="list-tag">
                <view class="state al-center">{{item.state}}</view>
                <view class="join al-center"><text class="list-num">{{item.num}}</text>人报名</view>
            </view>
            <view class="list-info"><text>{{item.address}}</text>|<text>{{item.time}}</text></view>
        </view>
    </view>
</block>
<view class="section">
    <text>到底啦</text>
</view>

在js文件的里面模拟假的数据

/**
     * 页面的初始数据
     */
    data: {
        tabs: ['会议中', '已完成', '已取消', '全部会议'],
        lists: [
            {
                'id': '1',
                'image': '/static/persons/1.jpg',
                'title': '对话产品总监 | 深圳·北京PM大会 【深度对话小米/京东/等产品总监】',
                'num': '304',
                'state': '进行中',
                'time': '10月09日 17:59',
                'address': '深圳市·南山区'
            },
            {
                'id': '1',
                'image': '/static/persons/2.jpg',
                'title': 'AI WORLD 2016世界人工智能大会',
                'num': '380',
                'state': '进行中',
                'time': '10月09日 17:39',
                'address': '北京市·朝阳区'
            },
            {
                'id': '1',
                'image': '/static/persons/3.jpg',
                'title': 'H100太空商业大会',
                'num': '500',
                'state': '进行中',
                'time': '10月09日 17:31',
                'address': '大连市'
            },
            {
                'id': '1',
                'image': '/static/persons/4.jpg',
                'title': '报名年度盛事,大咖云集!2016凤凰国际论坛邀您“与世界对话”',
                'num': '150',
                'state': '进行中',
                'time': '10月09日 17:21',
                'address': '北京市·朝阳区'
            },
            {
                'id': '1',
                'image': '/static/persons/5.jpg',
                'title': '新质生活 · 品质时代 2016消费升级创新大会',
                'num': '217',
                'state': '进行中',
                'time': '10月09日 16:59',
                'address': '北京市·朝阳区'
            }
        ],
        lists1: [
            {
                'id': '1',
                'image': '/static/persons/1.jpg',
                'title': '对话产品总监 | 深圳·北京PM大会 【深度对话小米/京东/等产品总监】',
                'num': '304',
                'state': '已结束',
                'time': '10月09日 17:59',
                'address': '深圳市·南山区'
            },
            {
                'id': '1',
                'image': '/static/persons/2.jpg',
                'title': 'AI WORLD 2016世界人工智能大会',
                'num': '380',
                'state': '已结束',
                'time': '10月09日 17:39',
                'address': '北京市·朝阳区'
            },
            {
                'id': '1',
                'image': '/static/persons/3.jpg',
                'title': 'H100太空商业大会',
                'num': '500',
                'state': '已结束',
                'time': '10月09日 17:31',
                'address': '大连市'
            }
        ],
        lists2: [
            {
                'id': '1',
                'image': '/static/persons/1.jpg',
                'title': '对话产品总监 | 深圳·北京PM大会 【深度对话小米/京东/等产品总监】',
                'num': '304',
                'state': '已取消',
                'time': '10月09日 17:59',
                'address': '深圳市·南山区'
            },
            {
                'id': '1',
                'image': '/static/persons/2.jpg',
                'title': 'AI WORLD 2016世界人工智能大会',
                'num': '380',
                'state': '已取消',
                'time': '10月09日 17:39',
                'address': '北京市·朝阳区'
            }
        ],
        lists3: [
            {
                'id': '1',
                'image': '/static/persons/1.jpg',
                'title': '对话产品总监 | 深圳·北京PM大会 【深度对话小米/京东/等产品总监】',
                'num': '304',
                'state': '进行中',
                'time': '10月09日 17:59',
                'address': '深圳市·南山区'
            },
            {
                'id': '1',
                'image': '/static/persons/2.jpg',
                'title': 'AI WORLD 2016世界人工智能大会',
                'num': '380',
                'state': '已结束',
                'time': '10月09日 17:39',
                'address': '北京市·朝阳区'
            },
            {
                'id': '1',
                'image': '/static/persons/3.jpg',
                'title': 'H100太空商业大会',
                'num': '500',
                'state': '进行中',
                'time': '10月09日 17:31',
                'address': '大连市'
            },
            {
                'id': '1',
                'image': '/static/persons/4.jpg',
                'title': '报名年度盛事,大咖云集!2016凤凰国际论坛邀您“与世界对话”',
                'num': '150',
                'state': '已结束',
                'time': '10月09日 17:21',
                'address': '北京市·朝阳区'
            },
            {
                'id': '1',
                'image': '/static/persons/5.jpg',
                'title': '新质生活 · 品质时代 2016消费升级创新大会',
                'num': '217',
                'state': '进行中',
                'time': '10月09日 16:59',
                'address': '北京市·朝阳区'
            }
        ]
    }

在下面编写一个方法,用来获取对应的数据

 tabsItemChange(e) {
        console.log(e)
        let tolists;
        if (e.detail.index == 0) {
            tolists = this.data.lists;
        } else if (e.detail.index == 1) {
            tolists = this.data.lists1;
        } else if (e.detail.index == 2) {
            tolists = this.data.lists2;
        } else {
            tolists = this.data.lists3;
        }
        this.setData({
            lists: tolists
        })
    }

完整代码

// pages/meeting/list/list.js
Page({

    /**
     * 页面的初始数据
     */
    data: {
        tabs: ['会议中', '已完成', '已取消', '全部会议'],
        lists: [
            {
                'id': '1',
                'image': '/static/persons/1.jpg',
                'title': '对话产品总监 | 深圳·北京PM大会 【深度对话小米/京东/等产品总监】',
                'num': '304',
                'state': '进行中',
                'time': '10月09日 17:59',
                'address': '深圳市·南山区'
            },
            {
                'id': '1',
                'image': '/static/persons/2.jpg',
                'title': 'AI WORLD 2016世界人工智能大会',
                'num': '380',
                'state': '进行中',
                'time': '10月09日 17:39',
                'address': '北京市·朝阳区'
            },
            {
                'id': '1',
                'image': '/static/persons/3.jpg',
                'title': 'H100太空商业大会',
                'num': '500',
                'state': '进行中',
                'time': '10月09日 17:31',
                'address': '大连市'
            },
            {
                'id': '1',
                'image': '/static/persons/4.jpg',
                'title': '报名年度盛事,大咖云集!2016凤凰国际论坛邀您“与世界对话”',
                'num': '150',
                'state': '进行中',
                'time': '10月09日 17:21',
                'address': '北京市·朝阳区'
            },
            {
                'id': '1',
                'image': '/static/persons/5.jpg',
                'title': '新质生活 · 品质时代 2016消费升级创新大会',
                'num': '217',
                'state': '进行中',
                'time': '10月09日 16:59',
                'address': '北京市·朝阳区'
            }
        ],
        lists1: [
            {
                'id': '1',
                'image': '/static/persons/1.jpg',
                'title': '对话产品总监 | 深圳·北京PM大会 【深度对话小米/京东/等产品总监】',
                'num': '304',
                'state': '已结束',
                'time': '10月09日 17:59',
                'address': '深圳市·南山区'
            },
            {
                'id': '1',
                'image': '/static/persons/2.jpg',
                'title': 'AI WORLD 2016世界人工智能大会',
                'num': '380',
                'state': '已结束',
                'time': '10月09日 17:39',
                'address': '北京市·朝阳区'
            },
            {
                'id': '1',
                'image': '/static/persons/3.jpg',
                'title': 'H100太空商业大会',
                'num': '500',
                'state': '已结束',
                'time': '10月09日 17:31',
                'address': '大连市'
            }
        ],
        lists2: [
            {
                'id': '1',
                'image': '/static/persons/1.jpg',
                'title': '对话产品总监 | 深圳·北京PM大会 【深度对话小米/京东/等产品总监】',
                'num': '304',
                'state': '已取消',
                'time': '10月09日 17:59',
                'address': '深圳市·南山区'
            },
            {
                'id': '1',
                'image': '/static/persons/2.jpg',
                'title': 'AI WORLD 2016世界人工智能大会',
                'num': '380',
                'state': '已取消',
                'time': '10月09日 17:39',
                'address': '北京市·朝阳区'
            }
        ],
        lists3: [
            {
                'id': '1',
                'image': '/static/persons/1.jpg',
                'title': '对话产品总监 | 深圳·北京PM大会 【深度对话小米/京东/等产品总监】',
                'num': '304',
                'state': '进行中',
                'time': '10月09日 17:59',
                'address': '深圳市·南山区'
            },
            {
                'id': '1',
                'image': '/static/persons/2.jpg',
                'title': 'AI WORLD 2016世界人工智能大会',
                'num': '380',
                'state': '已结束',
                'time': '10月09日 17:39',
                'address': '北京市·朝阳区'
            },
            {
                'id': '1',
                'image': '/static/persons/3.jpg',
                'title': 'H100太空商业大会',
                'num': '500',
                'state': '进行中',
                'time': '10月09日 17:31',
                'address': '大连市'
            },
            {
                'id': '1',
                'image': '/static/persons/4.jpg',
                'title': '报名年度盛事,大咖云集!2016凤凰国际论坛邀您“与世界对话”',
                'num': '150',
                'state': '已结束',
                'time': '10月09日 17:21',
                'address': '北京市·朝阳区'
            },
            {
                'id': '1',
                'image': '/static/persons/5.jpg',
                'title': '新质生活 · 品质时代 2016消费升级创新大会',
                'num': '217',
                'state': '进行中',
                'time': '10月09日 16:59',
                'address': '北京市·朝阳区'
            }
        ]
    },
    tabsItemChange(e) {
        console.log(e)
        let tolists;
        if (e.detail.index == 0) {
            tolists = this.data.lists;
        } else if (e.detail.index == 1) {
            tolists = this.data.lists1;
        } else if (e.detail.index == 2) {
            tolists = this.data.lists2;
        } else {
            tolists = this.data.lists3;
        }
        this.setData({
            lists: tolists
        })
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {

    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    }
})

【微信小程序】6天精准入门(第4天:自定义组件及案例界面)附源码,# 微信小程序,微信小程序,小程序

2、投票

绑定自定义组件

json

{
    "usingComponents": {
       "tabs" : "/components/tabs/tabs"
    }
}

wxml

<!--pages/vote/list/list.wxml-->
<!-- <text>投票</text> -->
<!-- <tabs inner-text='6666'></tabs> -->

<tabs tabList="{{tabs}}" bindtabsItemChange="tabsItemChange">
    <view class="search-container">
        <input class="search-input" bindinput="searchInputOne" placeholder="投票标题                    🔍" />
        <input class="search-input" bindinput="searchInputTwo" placeholder="投票选项                 🔍" />
    </view>
</tabs>
<!-- height: 120rpx;  -->
<view style="background-color: #aaa;"></view>

<view>
    <block wx:for-items="{{lists}}" wx:for-item="item" wx:key="item.id">
        <view class="list" data-id="{{item.id}}">
            <view class="list-img al-center">
                <image class="video-img" mode="scaleToFill" src="{{item.image}}"></image>
            </view>
            <view class="list-detail">
                <view class="list-title"><text><text style="margin-right: 13rpx;"> 发 起 人</text> : {{item.name}}</text></view>
                <view class="list-title"><text>会议名称 : {{item.title}}</text></view>
                <view class="list-title"><text>投票标题 : [ {{item.vote}} ]</text></view>
                <view class="list-tag">
                    <view class="state al-center">{{item.state}}</view>
                    <view class="join al-center"><text class="list-count">{{item.count}}</text>人参与投票</view>
                </view>
                <view class="list-info"><text style="font-weight: bold;">地址:</text><text>{{item.address}}</text> <text style="float: right;">{{item.time}}</text> </view>
                <view> <button class="btn">参与</button></view>
            </view>
        </view>
    </block>
    <view class="section bottom-line">
        <text>到底啦</text>
    </view>
</view>

js

// pages/vote/list/list.js
Page({

    /**
     * 页面的初始数据
     */
    data: {
        tabs: ['全部', '已投票', '未投票'],
        lists: [
            {
                'id': '1',
                'image': '/static/persons/3.jpg',
                'name': '张三',
                'title': '报名年度盛事,大咖云集!2016凤凰国际论坛邀您“与世界对话”',
                'vote': '是否认同与京东进行产品合作',
                'count': '304',
                'state': '未投票',
                'time': '10月09日 17:59',
                'address': '深圳市·南山区'
            },
            {
                'id': '2',
                'image': '/static/persons/6.jpg',
                'name': '刘兵',
                'title': 'AIWORLD人工智能大会',
                'vote': '是否投资AI发展',
                'count': '480',
                'state': '已投票',
                'time': '10月09日 17:39',
                'address': '北京市·朝阳区'
            },
            {
                'id': '3',
                'image': '/static/persons/3.jpg',
                'name': '李四',
                'title': '报名年度盛事,大咖云集!2016凤凰国际论坛邀您“与世界对话”',
                'vote': '是否太空商业进行合作',
                'count': '500',
                'state': '未投票',
                'time': '10月09日 17:31',
                'address': '大连市'
            },
            {
                'id': '4',
                'image': '/static/persons/7.jpg',
                'name': ' 王五 ',
                'title': '报名年度盛事,大咖云集!2016凤凰国际论坛邀您“与世界对话”',
                'vote': '是否对本次创新持续升级',
                'count': '217',
                'state': '已投票',
                'time': '11月20日 16:59',
                'address': '北京市·朝阳区'
            }
        ],
        lists1: [
            {
                'id': '1',
                'image': '/static/persons/4.jpg',
                'name': '赵六',
                'title': '新质生活 · 品质时代 2016消费升级创新大会',
                'vote': '是否认同与京东进行产品合作',
                'count': '304',
                'state': '已投票',
                'time': '10月09日 17:59',
                'address': '深圳市·南山区'
            },
            {
                'id': '2',
                'image': '/static/persons/2.jpg',
                'name': '管理员',
                'title': '新质生活 · 品质时代 2016消费升级创新大会',
                'vote': '是否投资AI发展',
                'count': '480',
                'state': '已投票',
                'time': '10月09日 17:39',
                'address': '北京市·朝阳区'
            },
            {
                'id': '3',
                'image': '/static/persons/3.jpg',
                'name': '张三',
                'title': 'H100太空商业大会',
                'vote': '是否太空商业进行合作',
                'count': '500',
                'state': '已投票',
                'time': '10月09日 17:31',
                'address': '大连市'
            },
            {
                'id': '4',
                'image': '/static/persons/5.jpg',
                'name': ' 李四 ',
                'title': 'CSDN消费升级创新大会',
                'vote': '是否对本次创新持续升级',
                'count': '217',
                'state': '已投票',
                'time': '11月20日 16:59',
                'address': '北京市·朝阳区'
            }
        ],
        lists2: [
            {
                'id': '1',
                'image': '/static/persons/4.jpg',
                'name': '张三',
                'title': '深圳·北京PM大会',
                'vote': '是否认同与京东进行产品合作',
                'count': '422',
                'state': '未投票',
                'time': '10月09日 17:59',
                'address': '深圳市·南山区'
            },
            {
                'id': '2',
                'image': '/static/persons/6.jpg',
                'name': '李四',
                'title': 'AIWORLD人工智能大会',
                'vote': '是否投资AI发展',
                'count': '377',
                'state': '未投票',
                'time': '10月09日 17:39',
                'address': '北京市·朝阳区'
            },
            {
                'id': '3',
                'image': '/static/persons/3.jpg',
                'name': '王五',
                'title': 'H100太空商业大会',
                'vote': '是否太空商业进行合作',
                'count': '463',
                'state': '未投票',
                'time': '10月09日 17:31',
                'address': '大连市'
            },
            {
                'id': '4',
                'image': '/static/persons/5.jpg',
                'name': ' K&赵六 ',
                'title': '2023消费升级创新大会',
                'vote': '是否对本次创新持续升级',
                'count': '543',
                'state': '未投票',
                'time': '11月20日 16:59',
                'address': '北京市·朝阳区'
            }
        ]

    },

    tabsItemChange(e) {
        console.log(e.detail);
        let tolists;
        if (e.detail.index == 0) {
            tolists = this.data.lists;
        } else if (e.detail.index == 1) {
            tolists = this.data.lists1;
        } else if (e.detail.index == 2) {
            tolists = this.data.lists2;
        } else {
            tolists = this.data.lists;
        }
        this.setData({
            lists: tolists
        })
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {

    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    }
})

wxss样式

/* pages/vote/list/list.wxss */

  .search-container {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 10px;
    background-color: #ffffff;
    border: cornsilk;
  }

  .search-input {
    width: 45%;
    padding: 8px;
    border-radius: 20px;
    border: 1px solid rgb(255, 255, 255);
    font-size: 14px;
    transition: border-color 0.3s;
    border: cornsilk;
  }

  .search-input:focus {
    outline: none;
    border-color: #51a7f9;
  }

  .search-input::placeholder {
    color: #999;
  }

  .search-input::-webkit-input-placeholder {
    color: #999;
  }

  .search-input::-moz-placeholder {
    color: #999;
  }

  .search-input:-ms-input-placeholder {
    color: #999;
  }
  /* .search-container {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 10px;
    background-color: #f0f0f0;
  }

  .search-input {
    width: 45%;
    padding: 8px;
    border-radius: 4px;
    border: 1px solid #ccc;
    font-size: 14px;
  } */
.list {
    display: flex;
    flex-direction: row;
    width: 100%;
    padding: 0 20rpx 0 0;
    background-color: seashell;
    border-bottom: 1px solid #cecece;
    margin-bottom: 5rpx;
    height: 350rpx;
}

.list-img {
    display: flex;
    margin: 10rpx 10rpx;
    width: 160rpx;
    height: 250rpx;
    justify-content: center;
    align-items: center;
    flex-direction: column;
}

.list-img .video-img {
    width: 140rpx;
    height: 160rpx;
    border-radius: 6px;
}

.list-detail {
    margin: 10rpx 10rpx;
    display: flex;
    flex-direction: column;
    width: 600rpx;
    height: 300rpx;
}

.list-title text {
    font-size: 9pt;
    color: #333;
    font-weight: bold;
}

.list-detail {
    display: flex;
    height: 100rpx;
}

.list-tag {
    display: flex;
}

.state {
    font-size: 9pt;
    color: blue;
    width: 120rpx;
    height: 40rpx;
    border: 1px solid blue;
    border-radius: 2px;
    margin: 10rpx 0rpx;
    display: flex;
    justify-content: center;
    align-items: center;
}

.join {
    font-size: 11pt;
    color: #bbb;
    margin-left: 20rpx;
    display: flex;
    justify-content: center;
    align-items: center;
}

.list-count {
    margin-right: 10rpx;
    font-size: 11pt;
    color: red;
}

.list-info {
    font-size: 9pt;
    color: #bbb;
}

.btn {
    /* width: 10rpx;
    height: 40rpx;
    background-color: #3388ff;
    color: #fff;
    text-align: center;
    font-size: 16rpx;
    display: flex;
    justify-content: center;
    align-items: center; */

    background-color: #3388ff;
      color: #fff;
      border-radius: 4rpx;
      font-size: 16rpx;
      padding: 10rpx 20rpx;

      /* background-color: #3388ff;
      color: #fff;
      border-color: #3388ff; */


      /* width: 100rpx;
      height: 40rpx;
      border: 1rpx solid #3388ff;
      border-radius: 4rpx;
      font-size: 16rpx;
      background-color: #3388ff;
      color: #fff;
      outline: none;
      box-shadow: 0 0 5rpx #3388ff; */

      /* width: 60rpx;
      height: 30rpx;
      border-radius: 4rpx;
      font-size: 16rpx; */

      /* width: 40rpx;
      height: 40rpx;
      background-color: transparent;
      border: none;
      font-size: 24rpx;
      color: #666; */


}

.bottom-line {
    display: flex;
    height: 60rpx;
    justify-content: center;
    align-items: center;
    background-color: #f3f3f3;
}

.bottom-line text {
    font-size: 9pt;
    color: #666;
}

【微信小程序】6天精准入门(第4天:自定义组件及案例界面)附源码,# 微信小程序,微信小程序,小程序

3、个人中心

wxml

<!--pages/ucenter/index/index.wxml-->
<!-- <text>个人中心</text> -->
<view class="user">
    <image class="user-img"  src="/static/persons/1.jpg"></image>
    <view class="user-name">无法自律的人</view>
    <text class="user-state">状态:😊</text>
    <text class="user-up">修改></text>
</view>
<view class="cells">
    <view class="cell-items">
        <image src="/static/tabBar/coding-active.png" class="cell-items-icon"></image>
        <text class="cell-items-title">我主持的会议</text>
        <text class="cell-items-num">99+</text>
        <text class="cell-items-detail">🆗</text>
    </view>
    <view style="height: 5rpx;background-color: rgba(135, 206, 250, 0.075);"></view>
    <view class="cell-items">
        <image src="/static/tabBar/sdk.png" class="cell-items-icon"></image>
        <text class="cell-items-title">我参与的会议</text>
        <text class="cell-items-num">99+</text>
        <text class="cell-items-detail">❤</text>
    </view>
</view>
<view style="height: 27rpx;background-color: rgba(135, 206, 250, 0.075);"></view>
<view class="cells">
    <view class="cell-items">
        <image src="/static/tabBar/sdk.png" class="cell-items-icon"></image>
        <text class="cell-items-title">我发布的投票</text>
        <text class="cell-items-num">89</text>
        <text class="cell-items-detail">👍</text>
    </view>
    <view style="height: 5rpx;background-color: rgba(135, 206, 250, 0.075);"></view>
    <view class="cell-items">
        <image src="/static/tabBar/coding-active.png" class="cell-items-icon"></image>
        <text class="cell-items-title">我参与的投票</text>
        <text class="cell-items-num">8</text>
        <text class="cell-items-detail">></text>
    </view>
</view>
<view style="height: 27rpx;background-color: rgba(135, 206, 250, 0.075);"></view>
<view class="cells">
    <view class="cell-items">
        <image src="/static/tabBar/template.png" class="cell-items-icon"></image>
        <text class="cell-items-title">信息</text>
        <text class="cell-items-ion">></text>
    </view>
    <view style="height: 5rpx;background-color: rgba(135, 206, 250, 0.075);"></view>
    <view class="cell-items">
        <image src="/static/tabBar/component.png" class="cell-items-icon"></image>
        <text class="cell-items-title">设置</text>
        <text class="cell-items-ion">></text>
    </view>
</view>

wxss

/* pages/ucenter/index/index.wxss */
Page {
    background-color: rgba(135, 206, 250, 0.075);
}

.user {
    display: flex;
    width: 100%;
    align-items: center;
    background-color: white;
    margin-bottom: 28rpx;
}

.user-img {
    height: 170rpx;
    width: 170rpx;
    margin: 30rpx;
    border: 1px solid #cdd7ee;
    border-radius: 6px;
}

.user-name {
    width: 380rpx;
    margin-left: 20rpx;
    font-weight: 550;
}

.user-state {
    color: rgb(136, 133, 133);
}

.user-up {
    color: rgb(136, 133, 133);
}

.cells {
    background-color: white;
}

.cell-items {
    display: flex;
    align-items: center;
    height: 110rpx;
}

.cell-items-title {
    width: 290rpx;
}

.cell-items-icon {
    width: 50rpx;
    height: 50rpx;
    margin: 20rpx;
}

.cell-items-num {
    padding-left: 30rpx;
    margin-left: 200rpx;
    width: 70rpx;
}

.cell-items-ion {
    margin-left: 295rpx;
}

【微信小程序】6天精准入门(第4天:自定义组件及案例界面)附源码,# 微信小程序,微信小程序,小程序文章来源地址https://www.toymoban.com/news/detail-719585.html

到了这里,关于【微信小程序】6天精准入门(第4天:自定义组件及案例界面)附源码的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • 微信小程序入门与实战之电影页面与实战自定义组件

    Movie自定义组件包含三部分:图片、标题和图片,评分可以再单独写一个组件。 实现代码: css代码中我们通过title里面的属性设置文字省略效果,为了能够直观地感受到效果我们必须给container设置宽度,当文字超过宽度的时候就会显示省略号,效果如下图所示: 首先在自定义

    2024年02月08日
    浏览(36)
  • 【微信小程序】6天精准入门(第1天:小程序入门)

             小程序是一种轻量级的应用程序 ,可以在移动设备上运行,不需要用户下载和安装。它们通常由企业或开发者开发,用于提供特定功能或服务。          微信小程序 (wei xin xiao cheng xu),简称小程序,英文名 Mini Program ,是一种不需要下载安装即可使用的应

    2024年02月08日
    浏览(28)
  • 【微信小程序】6天精准入门(第2天:小程序的视图层、逻辑层、事件系统及页面生命周期)

    框架的视图层由 WXML 与 WXSS 编写, 由组件来进行展示 。 将 逻辑层的数据反映成视图 ,同时将 视图层的事件发送给逻辑层 。 WXML (WeiXin Markup language) 用于描述页面的结构。 WXS (WeiXin Script) 是小程序的一套脚本语言,结合 WXML ,可以构建出 页面的结构 。 WXSS (WeiXin Style Sheet)

    2024年02月08日
    浏览(36)
  • 微信小程序自定义扫码界面

    因扫码功能需支持自定义输入,调用微信 scanCode 无法自定义界面补充输入框,所以使用原生组件 camera。 小程序自定义扫码演示 1、调用摄像头扫码 将媒体组件 camera 应用模式设置为 scanCode ,并绑定扫码识别成功方法 2、模拟扫码动画 为提升体验,模拟扫码动画 JS 代码 wxml

    2024年02月16日
    浏览(28)
  • 微信小程序首页、界面布局、自定义顶部(示例一)

    具体界面见下图: 如需界面中引用的图片文件和更多功能,请滑动至底部查看下载链接,可下载完整版,下载后直接使用微信开发者工具打开即可,完整版功能更详细呦。当前界面的布局样式代码如下(如存在不足之处,请根据具体需求,自行修改): 1、js代码: 2、wxml代

    2024年02月12日
    浏览(30)
  • 【微信小程序】自定义组件(三)

    插槽 1、什么是插槽 在自定义组件的wxml结构中,可以提供一个 solot 节点(插槽),用于承载组件使用者提供的wxml结构 2、单个插槽 在小程序中,默认每个自定义组件中只允许使用一个 slot 进行占位,这种个数上的限制叫做单个插槽。 3、定义多个插槽 父子组件之间的通信

    2024年02月04日
    浏览(36)
  • 微信小程序自定义组件标签

    目录 前言 1.创建一个components文件夹,用来放自定义组件标签

    2024年02月10日
    浏览(83)
  • 【微信小程序】自定义组件(一)

    组件的创建与引用 1、创建组件 在项目的根目录中,鼠标右键,创建 components - test 文件夹 在新建的components - test文件夹上,鼠标右键,点击\\\"新建Component\\\" 键入组件的名称之后回车,会自动生成组件对应的4个文件,后缀名分别为 js, json, .wxml 和.Wxss 注意:为了保证目录结构的清

    2024年02月05日
    浏览(39)
  • 【微信小程序】自定义组件(一)

    🎁 写在前面: 观众老爷们好呀,这里是前端小刘不怕牛牛频道,小程序系列文章又更新了呀。 今天牛牛带来的是微信小程序的自定义组件入门知识,赶紧拿起小本本做笔记呀! 1.1 介绍 自定义组件,就是开发者将页面的某个功能模块抽象化并提取出来的代码块,支持复用,

    2023年04月08日
    浏览(41)
  • 【微信小程序】自定义组件(二)

    🎁 写在前面: 观众老爷们好呀,这里是前端小刘不怕牛牛频道,小程序系列文章又更新了呀。 上文我们讲解了微信小程序自定义组件的入门知识,那么今天牛牛就来讲讲自定义组件的进阶知识吧,赶紧拿起小本本做笔记呀! 自定义组件的数据和方法在使用上,和 Vue 的组件

    2024年02月02日
    浏览(37)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包