微信小程序如何修改本地緩存key中單個(gè)數(shù)據(jù)的詳解
最近在做教師評(píng)教系統(tǒng),有一個(gè)‘個(gè)人信息'頁(yè)面中有個(gè)編輯修改郵箱的功能,本來(lái)想得很簡(jiǎn)單,結(jié)果進(jìn)坑了,搞了好久才出來(lái)。
我想實(shí)現(xiàn)的效果是點(diǎn)擊下圖左側(cè)郵箱,然后進(jìn)入右側(cè)頁(yè)面,進(jìn)行郵箱的修改,點(diǎn)擊提交后跳轉(zhuǎn)到左側(cè)頁(yè)面,同時(shí)郵箱也發(fā)生改變。

點(diǎn)擊‘我的'時(shí),我讓它從控制臺(tái)打印出student緩存中傳過(guò)來(lái)的數(shù)據(jù),如下:
{no: "1635050601", name: "張三", sex: "", email: "123@qq.com", classid: "100000-1602", …}
classid:"100000-1602"
classname:"16級(jí)PHP2"
departmentid:"100000"
departmentname:"軟件學(xué)院"
name:"張三"
no:"1635050601"
sex:""
然后我添加郵箱后,后臺(tái)接口寫了方法讓email的值直接存到student中,但是如果初次添加email的話可以實(shí)現(xiàn),第二次修改email的話,就得想想該怎么從student里只修改email的值。
//表單提交
formSubmit: function (e) {
console.log(e.detail.value);
var pwd = e.detail.value.pwd;
var email = e.detail.value.email;
if (pwd == '') {
wx.showToast({
title: '密碼不能為空',
icon: 'none',
duration: 1000,
})
}else if (email == '') {
wx.showToast({
title: '郵箱不能為空',
icon: 'none',
duration: 1000,
})
}else {
//post方式提交
wx.request({
url: app.globalData.url.bindemail,
method: "POST",
data: {
no: this.data.no,
pwd: pwd,
email: email
},
header: {
"Content-Type": "application/x-www-form-urlencoded"
},
success: function (res) {
// console.log(res);
if(res.data.error == true){
wx.showToast({
title: res.data.msg,
icon: 'none',
duration: 1000,
})
}else{
//修改email
var _student = wx.getStorageSync('student');
_student.email = email;
wx.setStorageSync('student', _student);
wx.showToast({
title: res.data.msg,
icon: 'success',
duration: 2000,
success: function () {
setTimeout(function () {
wx.reLaunch({
url: '../myinfo/myinfo',
})
}, 2000)
}
})
}
},
})
}
},
這里我們用下邊方法從student里只修改email的值。
//修改email
var _student = wx.getStorageSync('student');
_student.email = email;
wx.setStorageSync('student', _student);
wx.setStorageSync(KEY,DATA)
將 data 存儲(chǔ)在本地緩存中指定的 key 中,會(huì)覆蓋掉原來(lái)該 key 對(duì)應(yīng)的內(nèi)容,這是一個(gè)同步接口。
wx.getStorageSync(KEY)
從本地緩存中同步獲取指定 key 對(duì)應(yīng)的內(nèi)容。
如有問(wèn)題或補(bǔ)充,歡迎小伙伴們留言哦~期待與你一同學(xué)習(xí),共同進(jìn)步!?。?br />
以上所述是小編給大家介紹的微信小程序如何修改本地緩存key中單個(gè)數(shù)據(jù)詳解整合,希望對(duì)大家有所幫助,如果大家有任何疑問(wèn)請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
相關(guān)文章
js 數(shù)組當(dāng)前行添加數(shù)據(jù)方法詳解
這篇文章主要介紹了js 數(shù)組當(dāng)前行添加數(shù)據(jù)方法詳解,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-07-07
深入認(rèn)識(shí)javascript中的eval函數(shù)
發(fā)現(xiàn)為本文起一個(gè)合適的標(biāo)題還不是那么容易,呵呵,所以在此先說(shuō)明下本文的兩個(gè)目的.2009-11-11
Code:loadScript( )加載js的功能函數(shù)
Code:loadScript( )加載js的功能函數(shù)...2007-02-02
輕松理解Javascript變量的相關(guān)問(wèn)題
這篇文章主要給大家介紹了關(guān)于Javascript變量的相關(guān)問(wèn)題,文中給出了詳細(xì)的介紹和示例代碼,相信對(duì)大家的理解和學(xué)習(xí)具有一定的參考借鑒價(jià)值,有需要的朋友們下面來(lái)一起看看吧。2017-01-01
如何去除js中的json存在的轉(zhuǎn)義字符\問(wèn)題
這篇文章主要介紹了如何去除js中的json存在的轉(zhuǎn)義字符\問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-09-09

