Get the App
SLTechnology News&Howtos  ›  Development  › 

How does WeChat Mini Programs extract common functions to util.js

Shulou Source: shulou.com Published: 2022-05-31 21:16:28 10月04日 Update

In this article, the editor introduces in detail "WeChat Mini Programs how to extract the common function to the util.js", the content is detailed, the steps are clear, and the details are handled properly. I hope that this "WeChat Mini Programs how to extract the common function to the util.js" article can help you solve your doubts, following the editor's ideas slowly in depth, together to learn new knowledge.

In Mini Program, a tool file utils is defined. The js of this file is designed to be valid within this file. When other sub-pages want to call the js method or variable, you need two steps:

1: in the js file where utils is called, the model is output in an object-oriented way:

Module.exports= {name of function to be called: name of function to be called}

2: modularize the js file that introduces utils into the js file to be called

Var object=require ("address of the js file where utils was called")

You can output object to see the method being called.

Examples are as follows:

Js called in utils:

Var URl=' http://123.23.169'; var getImageurl=function (imageurl) {return URl+imageurl } / / the function or variable to reference this file, in addition to modularization in the js file to be referenced (var utils=require ('js address')), / / in the referenced js, the output function is as follows: module.exports= {URl:URl,// function to be referenced as an object-oriented variable

The js file to call:

/ / get the function in the tool utils tool js. First, modularize the js address reqiure ('js address') in utils into an object-oriented var utils=require ('.. /.. / utils/app.js') / / console.log (utils) to view the obtained function console.log (utils.getImageurl ('iamgeaaddress.png')).

Output result:

Supplement: the creation and use of the tool library

Create a common folder

Create a utils folder in the common folder

Create a util.js in the utils folder

/ / tool function tool () {console.log ('i am a tool function.');} module.exports = {tool: tool}

Call in the specific page js

The first step is to introduce the tool file.

Const util = require ('.. /.. / common/utils/util')

Then use it in a specific location.

OnLoad: function (options) {util.tool ();...} i am a tool function.

It's kind of like common.php in php, where there are a lot of general function libraries. In fact, whether it is the front end or the back end. Some ideas are the same.

After reading this, the article "WeChat Mini Programs how to extract common functions to util.js" has been introduced. If you want to master the knowledge points of this article, you still need to practice and use it yourself to understand it. If you want to know more about related articles, welcome to follow the industry information channel.

Tags: Files functions tools programs addresses folders outputs public variables objects articles modules contents names methods steps pages creation proper valid Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno macOS MariaDB Linux Apple vpn