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)

不言
不言Original
2018-09-06 13:46:133557browse

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!

Statement:
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