Layui 自定义模块的使用方式

使用layui.define()方法来扩展模块、当然模块中你也可以使用layui的其他方法、如下

layui.define('layer', callback);

在定义扩展模块的时候、我需要使用layui的layer模块、然后在回调函数中定义自己的方法

layui.define(["layer","jquery"],function (exports) {
   var obj = {
       login : function (url,data,$,targetUrl) {
           $.post(url,{code:data.code},function (res) {
             if (res.code&&res.code==400){
                 layer.msg(res.msg,{icon:1},function () {
                     window.location.href = targetUrl;
                 });
             }else{
                 layer.msg(res.msg,{icon:1},function () {
                     window.location.href = targetUrl;
                 });
             }
           });
       }
   }
   exports("login",obj);
});

上述代码中定义了一个login模块、以便在我登录的时候、不需要写过度的代码即可实现登录、让页面看起来更清爽【无任何杂质】

那么模块定义完了、怎么使用呢?

<script type="text/javascript">
    layui.config({
        base: '/static/admin/js/module/'//模块存放的目录
    }).use(['jquery','element','form','login'],function () {
        var $ = layui.jquery,
            form = layui.form,
            element = layui.element,
            login = layui.login;
        form.on("submit(subBtn)",function (data) {
            //获取表单的值
            var field = data.field;
            login.login("{:url('Login/doLogin')}",field,$,"{:url('Index/index')}");
            return false;
        });
    });
</script>

 自定义模块

require(['jquery', 'jquery-form'], function ($) {
    layui.define(["layer", "jquery"], function (exports) {
        var layer = layui.layer,
            $ = layui.jquery;

        var topLayer = {
            setTopDocument: function (index, results) {
                if (!index) {
                    return;
                }
                if (!results) {
                    return;
                }
                var html = '<form id="topLayerForm-' + index + '" style="display:none;">';
                for (var result in results) {
                    html += '<input id="' + result + '" name="' + result + '" value="' + results[result] + '">';
                }
                html += '</form>';
                $("body", window.top.document).append(html);
            },
            getTopDocument: function (index) {
                if (!index) {
                    return;
                }
                var value = $("#topLayerForm-" + index, window.top.document).serializeJSON();
                $("#topLayerForm", window.top.document).remove();
                return value;
            }
        }
        exports("topLayer", topLayer);
    });
});

实例

require(['jqueryBase64'], function () {
    layui.define(["layer", "jquery"], function (exports) {
        var layer = layui.layer,
            $ = layui.jquery;

        var showDetailMsg = {
            /*****店铺信息详情*****/
            showShopDetail: function (shopType, shopId) {
                var url = '/baseData/shopManage/shopDetail?shopType=' + shopType + '&shopId=' + shopId;
                url = $.base64.encode(url);
                var gourl = "/index/newIndex?childrenUrl=" + url + "&title=店铺信息详情";
                //抽检商品-添加抽检商品
                openTabatNewIndex('店铺信息详情', gourl, 'link')
            },
            /*****商品信息详情*****/
            commodityDatail: function (commodityId, shopType) {
                var url = '/baseData/shopManage/commodityDatail?commodityId=' + commodityId
                url = $.base64.encode(url);
                var gourl = "/index/newIndex?childrenUrl=" + url + "&title=商品信息详情";
                openTab('商品信息详情', gourl, 'link')
            }
        }
        exports("showDetailMsg", showDetailMsg);
    });
});
require(['jquery', 'jqueryBase64', 'jquery-form'], function ($) {

    layui.config({
        base: '/resource/scripts/module/'
    }).use(['layer', 'form', 'table', 'orgTree', 'showDetailMsg', 'laydate'], function () {
        var layer = layui.layer,
            table = layui.table,
            form = layui.form,
            laydate = layui.laydate,
            orgTree = layui.orgTree,
            upload = layui.upload,
            element = layui.element,
            showDetailMsg = layui.showDetailMsg,
            companyTagTree = layui.companyTagTree,
            $ = layui.jquery;

        //   成立时间
        var establishDateStart = laydate.render({
            elem: "#establishDateStart",
            trigger: 'click',
            done: function (value, date) {
                endMax = establishDateEnd.config.max;
                establishDateEnd.config.min = date;
                establishDateEnd.config.min.month = date.month - 1;
            }
        });
        // 结束时间
        var establishDateEnd = laydate.render({
            elem: "#establishDateEnd",
            trigger: 'click',
            done: function (value, date) {
                if ($.trim(value) == '') {
                    var curDate = new Date();
                    date = {'date': curDate.getDate(), 'month': curDate.getMonth() + 1, 'year': curDate.getFullYear()};
                }
                establishDateStart.config.max = date;
                establishDateStart.config.max.month = date.month - 1;
            }
        });
    });
});

 https://zhuanlan.zhihu.com/p/64123367

posted @ 2022-03-08 17:51  Bonnie_ξ  阅读(428)  评论(0编辑  收藏  举报