微信小程序动态生成view结构

295 阅读1分钟

需求描述

如下图所示,虽然现在只有两个控件,但是考虑到后期可能存在业务拓展,现需要使其动态生成。
在这里插入图片描述

需求分析

index.js中存featureList变量保存控件相关数据,记录name,func(点击事件)index.wxml页面遍历featureList,只需要写一个view结构即可。

//index.js
 featureList: [{
      key: '1',
      name: '卡券核销',
      func:"onVerifyCard"
    }, {
      key: '2',
      name: '核销记录',
      func:'onCheckVerifiedCards',
    }]
//index.wxml
<block wx:for-index="index" wx:for="{{featureList}}">
	<view class="storeItem" bindtap="{{item.func}}">{{item.name}}</view>
 </block>

效果

以下是实现的效果(界面正在完善)
在这里插入图片描述