前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >使用滴滴mpx 开发一个小程序组件

使用滴滴mpx 开发一个小程序组件

作者头像
yangdongnan
发布2019-03-28 16:10:02
1.1K0
发布2019-03-28 16:10:02
举报
文章被收录于专栏:日常记录日常记录

前文 准备工具 webstorm ide npm i @mpxjs/col 小程序中 组件和页面分的很明确, mpx中一样 createComponent 创建一个组件 createPage 创建一个页面 下面是一个上图下文的list组件

代码块

代码语言:javascript
复制
<template>
	<view class="grids">
		<view class="grids-item"
					wx:for="{{rData}}"
					wx:for-index="index"
					wx:for-item="item"
					wx:key="index"
					bind:tap="handleGridsItem">
			<view>
				<image class="grids-icon" src="{{item.icon}}"></image>
			</view>
			<view class="grids-text">
				{{item.name}}
			</view>
		</view>
	</view>
</template>
<script>
  import { createComponent } from '@mpxjs/core'
  
  createComponent({
    mixins : [],
    properties : {  //小程序中依靠properties来传递 父组件数据 如同 vue 中 props
      rData : {
        type : Array,
        default : () => []
      }
    },
    data : {},
    computed : {},
    ready () {},
    methods : {
      handleGridsItem ( evt ) {
         this.triggerEvent('click',{evt},{})  //组件事件触发监听
      }
    }
  })
</script>
<style lang="less">
	.grids {
		display: block;
		width: 100%;
		padding-top: 12px;
	}

	.grids-item {
		display: inline-block;
		width: 25%;
		text-align: center;
		padding: 12px 0;
		margin-bottom: 8px;
	}

	.grids-icon {
		width: 24px;
		height: 24px;
		vertical-align: middle;
	}

	.grids-text {
		margin-top: 5px;
		font-size: 14px;
		font-weight: 500;
	}
</style>

<script type="application/json">
	{}
</script>
  • ps:有了这个组件就可以做一些事情了

效果图

在这里插入图片描述
在这里插入图片描述

附父组件监听调用

代码语言:javascript
复制
<_grids rData="{{rDataList1}}" col="{{col}}" bind:click="handleGridsItem"></_grids>
mthods:{
	 handleGridsItem(item){
        	console.log(item);
        }
}
在这里插入图片描述
在这里插入图片描述
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2018年12月12日,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
相关产品与服务
云开发 CloudBase
云开发(Tencent CloudBase,TCB)是腾讯云提供的云原生一体化开发环境和工具平台,为200万+企业和开发者提供高可用、自动弹性扩缩的后端云服务,可用于云端一体化开发多种端应用(小程序、公众号、Web 应用等),避免了应用开发过程中繁琐的服务器搭建及运维,开发者可以专注于业务逻辑的实现,开发门槛更低,效率更高。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档