使用layui定义一个模块并使用的实例
2018-01-19 18:32
477 查看
在使用layui的过程,有几个方法需要被多个js引用,所以我按照文档,自定义了一个layui的模块,下面实现过程
先定义一个模块
定义方法使用
define中的第一个参数可以加载一些layui中内置的模块来使用,第二个回调方法中定义一些公共的方法
exports的第一参数是这个模块起的名字,第二个参数是一个对象该对象中定义了这三个方法
设置模块
定义模块使用
base定义的是定义模块的js所在目录
common是设置的别名,’common’是目录中的文件名(.js自动加)
//调用模块
先将common模块use出来,调用代码如上
先定义一个模块
//定义模块 layui.define(['form'], function(exports){ var form = layui.form; //只有执行了这一步,部分表单元素才会自动修饰成功 var $ = layui.$; var obj = { changeParentPlace: function (parentId,tagId,levelPlace) { $.ajax({ url:ctx + '/base/place/changeParentP 4000 lace', type:'get', data:{ parentId:parentId }, dataType:'json', success:function (data) { $("#" + tagId).empty(); $("#" + tagId).append('<option value="">请选择</option>'); if(data.result != null && data.result.length != 0) { $.each(data.result,function(i,place) { if(levelPlace == place.name) { $("#" + tagId).append('<option selected value="'+place.id+'">'+place.name+'</option>') }else { $("#" + tagId).append('<option value="'+place.id+'">'+place.name+'</option>') } }) } form.render('select'); //刷新select选择框渲染 } }) } } //输出模块 exports('common', obj); });
定义方法使用
layui.define
define中的第一个参数可以加载一些layui中内置的模块来使用,第二个回调方法中定义一些公共的方法
exports的第一参数是这个模块起的名字,第二个参数是一个对象该对象中定义了这三个方法
设置模块
//设置模块 layui.config({ base: ctx + '/js/app/modules/' //假设这是你存放拓展模块的根目录 }).extend({ //设定模块别名 common:'common' });
定义模块使用
layui.config
base定义的是定义模块的js所在目录
common是设置的别名,’common’是目录中的文件名(.js自动加)
//调用模块
layui.use(['common'], function(){ var common = layui.common; common.changeParentPlace($("#firstLevelId").val(),'secondLevelId'); });
先将common模块use出来,调用代码如上
common.changeParentPlace($("#firstLevelId").val(),'secondLevelId');和使用内置模块是一样的
相关文章推荐
- requirejs如何使用define定义一个模块
- -----------qt-----------C++的模板的使用, ---定义一个模块类--------
- 一个使用泛型堆栈模块创建的两个容纳不同类型数据的实例
- requirejs如何使用define定义一个模块
- requirejs如何使用define定义一个模块
- requirejs如何使用define定义一个模块
- 使用Object.definePropery方法定义一个只读对象实例
- requirejs如何使用define定义一个模块
- requirejs如何使用define定义一个模块
- requirejs如何使用define定义一个模块
- requirejs如何使用define定义一个模块
- 一步一步教你使用AgileEAS.NET基础类库进行应用开发-WinForm应用篇-实例一个模块(商品字典)
- requirejs如何使用define定义一个模块
- requirejs如何使用define定义一个模块
- requirejs如何使用define定义一个模块
- requirejs如何使用define定义一个模块
- requirejs如何使用define定义一个模块
- requirejs如何使用define定义一个模块
- requirejs如何使用define定义一个模块
- requirejs如何使用define定义一个模块