


The content of this article is about the method encapsulation (code example) of model.js in the mini program. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.
As we all know, small program codes are limited and cannot exceed 2M. Solving redundant codes is the top priority. This is the encapsulation of the model file, which is equivalent to the model class in PHP that operates the database, and its functions are also the same.
//这里是继承Base封装好的model类,其中是对get和post传输方式的封装 import { Base } from '../../utils/base.js'; class Index extends Base { constructor() { super(); } /**储存用户信息 */ //在下面的方法当中,get或者post传输的方式,和通过transfertype条件添加,例如:transfertype:‘post’ //而role:“域名下的类名后缀”,由于我是用了两个数据库,所以加了判断,如果不需要可以到base.js文件中修改 setUserInfo(sid, openid, callback) { var that = this; var param = { url: 'setUserInfo',//这里写自定义的接口方法名 data:{ sid: sid, openid: openid }, transfertype: 'post', sCallback: function (data) { callback && callback(data); } }; this.request(param); } }; //这里是暴露封装的文件名,相当于php中的model类 export { Index }; //这里是周期函数所在文件js,接收上面model类的方式 import { Index } from 'index-model.js'; var index = new Index(); //实例化 首页 对象 //在使用index里面的方法时,用法 index.setUserInfo(sid, openid,res=>{ //这里查看操作结果 console.log(res) })
Related recommendations:
Encapsulation method of common modules in Node.js_node.js
PHP encapsulation Usage examples of HttpClient class, encapsulating httpclient_PHP tutorial
The above is the detailed content of Method encapsulation of model.js in mini program (code example). For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment