温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

微信小程序如何制作小组件

发布时间:2021-06-08 15:00:25 来源:亿速云 阅读:329 作者:小新 栏目:移动开发

这篇文章给大家分享的是有关微信小程序如何制作小组件的内容。小编觉得挺实用的,因此分享给大家做个参考,一起跟随小编过来看看吧。

微信小程序 制作小组件

对于我们日常中一些公共的东西可以封装成组件,然后在各个页面使用。对于小程序,我们也可以封装我们需要的一些公共的东西。

这里我们讲解一个小插件。

微信小程序如何制作小组件

如上图所示,一个小插件,单击是展开,在单击关闭的时候,按钮关闭。

页面的WXML (APP.wxml)

<template name="widget-dialog-iconList">
  <view class="com-widget-iconList {{close==1?'hideImg':''}}" style="display:flex;flex-direction:row;">
    <view style="display:flex;flex-direction:row;">
      <view class="left-icon" style="display:flex;flex-direction:row;">
        <view class="left-circle"></view>
        <image class="icon1" src="http://m.dev.vd.cn/static/xcx/v1/goo/md_logo.png"></image>
      </view>
      <view class="middle_icon " style="display:flex;flex-direction:row;">
        <navigator url="../tua/home">
          <view class="section1">
            <view><image class="icon2" src="http://m.dev.vd.cn/static/xcx/v1/goo/firsticon.png"></image></view>
            <view class="text">首页</view>
          </view>
        </navigator>
        <navigator url="../ord/list">
          <view class="section2">
            <view><image class="icon2" src="http://m.dev.vd.cn/static/xcx/v1/goo/orderIcon.png"></image></view>
            <view class="text">订单</view>
          </view>
        </navigator>
        <navigator url="../usr/center">
          <view class="section3">
            <view><image class="icon3" src="http://m.dev.vd.cn/static/xcx/v1/goo/myself.png"></image></view>
            <view class="text">我的</view>
          </view>
        </navigator>
        <view class="right-icon" style="display:flex;flex-direction:row;">
          <image class="iconright" src="http://m.dev.vd.cn/static/xcx/v1/goo/delAllIcon.png" bindtap="closeAllIcon"></image>
        </view>
      </view>

    </view>
  </view>
  <view class="iconOnly {{close==0?'hideImg':''}}">
    <image class="iconOnlyPic" src="http://m.dev.vd.cn/static/xcx/v1/goo/md_logo.png" bindtap="showAllIcon"></image>
  </view>
</template>

这里主要是插件的压面展示效果,都写在<template>标签里面就可以了。

页面的JS (APP.js)

var iconList = {};    //设置一个对象名字存放数据
iconList.Wdg= {
      //存放要给VIEW层的页面数据,closeAllIcon ,showAllIcon 是对应的方法  
  data: {        
    index: 0,
    close:0
  },
  closeAllIcon: function(e){
      this.setData({
        close:1
      })
  },
  showAllIcon :function(e){
      this.setData({
        close:0
      })
  }
};

module.exports=iconList  //将接口的进行暴露,方便在外面调用

接下来封装好了,就是该怎么使用了。

在需要的WXML页面:

通过 引入斤页面,再通过

<template is="widget-dialog-iconList" data="{{你要传到页面的数据}}"></template>

进行使用。

在需要的WXML页面:

通过var iconList = require('../wdg/iconList');引入对应的JS

var util= require('../../util/util');
var Page = new util.Page({
  Wdgs: [iconList.Wdg]
});

引入对应文件。

感谢各位的阅读!关于“微信小程序如何制作小组件”这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,让大家可以学到更多知识,如果觉得文章不错,可以把它分享出去让更多的人看到吧!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI