公共的js
public.js
class publicMethod{
constructor(key, value) {
this.key = key;
this.value= value;
}
//不带返回值的方法
save(){
wx.setStorage({
key: this.key,
data: this.value
})
}
//携带返回值的方法
get(){
return new Promise((reslove, reject) => {
try {
wx.getStorage({
key: this.key,
success(res) {
console.log(res)
reslove(res)
},
fail(err) {
reslove([])
}
})
} catch (err) {
reslove([])
}
})
}
}
//方法导出
export {
publicMethod
}
//页面调用
page.js
import {publicMethod} from '../../utils/publicMethod.js'
//事件触发时候调用
onclick(){
const publicmethod= new publicMethod(key,value);
publicmethod.save();
publicmethod.get().then(res=>{
})
}
微信小城序中使用es6 class方法封装函数并调用
最新推荐文章于 2024-02-02 16:17:41 发布