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 WeChat Mini Programs extract common functions to util.js

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

Share

Shulou(Shulou.com)05/31 Report--

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.

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