Home WeChat Applet Mini Program Development Method encapsulation of model.js in mini program (code example)

Method encapsulation of model.js in mini program (code example)

Sep 06, 2018 pm 01:46 PM

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)
})
Copy after login

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!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Chat Commands and How to Use Them
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)