The content of this article is to share with you how to extract public functions from small programs to util.js. It has certain reference value. Friends in need can refer to it
In In the mini program, a tool file utils is defined. The js of this file is intended to be valid within this file. When other sub-pages want to call the js methods or variables in it, two steps are required:
1: In the js file called by utils, the object-oriented model output: module.exports={Name of the function to be called: Name of the function to be called};
2: In the Modularly introduce the js file of utils in the called js file var object=require("the js file address where utils is called"); You can output the object to see the called method;
Examples are as follows:
js called in utils:
var URl='http://123.23.169'; var getImageurl=function(imageurl){ return URl+imageurl; } // 要引用这个文件的函数或者变量,除了在要引用的的js文件中模块化之外(var utils=require('js地址')), // 在被引用的的js中要通过 module.exports={a:a}作为面向对象的变量输出函数如下: module.exports={ URl:URl,//要引用的函数 xx:xx getImageurl:getImageurl }
js file to be called :
// 获得工具utils工具js里面函数,先模块化引用utils里面的js地址 reqiure('js地址')成一个面向对象 var utils=require('../../utils/app.js') // console.log(utils) 可查看获得的函数 console.log(utils.getImageurl('iamgeaaddress.png'))
Output result:
#Related recommendations:
Little program The public js is extracted to the instance called in app.js
The above is the detailed content of How to extract public functions from applet to util.js. For more information, please follow other related articles on the PHP Chinese website!