首頁  >  文章  >  微信小程式  >  微信小程式公共組件的封裝製作方式

微信小程式公共組件的封裝製作方式

高洛峰
高洛峰原創
2017-02-27 14:24:473210瀏覽

在開發的過程中,我們常常會把一些公共的功能程式碼或效果,封裝成一個一個的元件,然後在需要用到的頁面中呼叫就可以了。
對於小程式的開發,我們也可以封裝一些公共的元件。

下面我們講解一個可以,點選圖示展開的選單,這樣功能元件。

 微信小程序公共组件的封装制作方式

 微信小程序公共组件的封装制作方式

如如上圖所示,一個小插件,點擊是展開,再點擊關閉的時候,按鈕關閉。
頁面的WXML (APP.wxml)

<template name="widget-dialog-iconList">
    <view class="com-widget-iconList {{close==1?&#39;hideImg&#39;:&#39;&#39;}}"  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://html51.com/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://html51.com/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://html51.com/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://html51.com/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://html51.com/static/xcx/v1/goo/delAllIcon.png" bindtap="closeAllIcon"></image>
                </view>
            </view>
 
        </view>
    </view>
    <view class="iconOnly {{close==0?&#39;hideImg&#39;:&#39;&#39;}}">
        <image class="iconOnlyPic" src="http://html51.com/static/xcx/v1/goo/md_logo.png" bindtap="showAllIcon"></image>
    </view>
</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(&#39;../../util/util&#39;);
var Page = new util.Page({
    Wdgs: [iconList.Wdg]
});

引入對應檔。

更多 微信小程式公共元件的封裝製作方式相關文章請關注PHP中文網!


陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn