Procedure small micro-channel data cache
Development setStorageSync used to achieve local data cache operation
(1) Click Cache Case:
<Button bindtap = " toStorage " > storage </ Button> // Local storage toStorage () { // Get the user data stored locally const appinstance = getApp () the console.log (appInstance.globalData); // print user information try { wx.setStorageSync ( ' the userInfo ' , appInstance.globalData); // playing box prompts wx.showToast ({ title: ' stored in the local cache data success ' , icon: ' none ' , DURATION: 2000 } });the catch (E) { // playing box prompts wx.showToast ({ title: ' stored in the local cache data fails, check the configuration, whether networking ' , icon: ' none ' , DURATION: 2000 }); } }
Click the button prompt cache fails: when prompted to click the button success
But in the daily development, as the user information data cache, the cache without manual operation, usually automatically cached page after loaded. So then modify it under
About app.js applet lifecycle and page.js page life cycle, as detailed in the next article micro letter applet life-cycle approach - Detailed
(2) Automatic local data cache
Page life cycle
Daily development in general will need to read the cached data into onready function in
Performs onLoad, onReady and onShow methods in order to open the applet after ①
② before and after the switching station performs onHide onShow and Methods,
③ applet execution method when the page is destroyed onUnload
the onReady: function () { // Get the user data stored locally const appinstance = getApp () the console.log (appInstance.globalData); // user information the try { wx.setStorageSync ( ' the userInfo ' , appInstance.globalData); // Usually immediately after loaded, the data cache will not be successful playing box prompts, so here remove } the catch (E) { // when fails, playing box prompts wx.showToast ({ title: ' stored in the local cache data failed, check configuration, whether the networking ' , icon: ' none ' , DURATION: 2000 }); } }
After this time the page is loaded, the user information will be automatically cached locally
.