Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How does layui define a module and use the

2025-04-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

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!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report