How does layui define a module and use the
This article is about how layui defines a module and uses it. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.
Define a module first
/ / definition module layui.define (['form'], function (exports) {var form = layui.form; / / only if this step is performed, some form elements will be automatically modified successfully var $= layui.$ Var obj = {changeParentPlace: function (parentId,tagId,levelPlace) {$.ajax ({url:ctx +'/ base/place/changeParentPlace', type:'get', data: {parentId:parentId}, dataType:'json' Success:function (data) {$("#" + tagId) .empty () $("#" + tagId) .append ('Please select') If (data.result! = null & & data.result.length! = 0) {$.each (data.result,function (I) Place) {if (levelPlace = = place.name) {$("#" + tagId) .append (''+ place.name+'')} else {$("#" + tagId) .append (''+ place.name+'') })} form.render ('select') / / Refresh select selector rendering}})} / / output module exports ('common', obj);})
Define methods using layui.define
The first parameter in define can be used by loading some modules built into layui, and some common methods are defined in the second callback method.
The first parameter of exports is the name of the module, and the second parameter is an object in which the three methods are defined
Setting module
/ / set module layui.config ({base: ctx +'/ js/app/modules/' / / suppose this is the root directory where you store the extension module}) .extend ({/ / set module alias common:'common'})
Define the module to use layui.config
Base defines the directory where the js that defines the module is located
Common is the alias of the setting, and 'common' is the file name in the directory (.js is automatically added)
Call module
Layui.use (['common'], function () {var common = layui.common; common.changeParentPlace ($("# firstLevelId"). Val (),' secondLevelId');})
First use the common module and call the code such as common.changeParentPlace ($("# firstLevelId"). Val (), 'secondLevelId'); it's the same as using the built-in module.
Thank you for reading! This is the end of the article on "how to define a module and use layui". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, you can share it for more people to see!