Home > Article > Web Front-end > How to cache data in uniapp
UniApp is a cross-platform development framework based on Vue.js and can be published to multiple platforms, such as iOS, Android and Web. In the development process, data caching is a very important link. This article will introduce how to cache data in UniApp, and attach corresponding code examples.
There are two main ways of data caching in UniApp: local storage and global variables.
1. Local Storage
Local storage saves data in the local storage space of the client so that the data can be restored when the user reopens the application. UniApp provides two APIs, uni.setStorageSync and uni.getStorageSync, for local storage.
uni.setStorageSync('username', 'Tom');
var username = uni.getStorageSync('username'); console.log(username); // 输出:Tom
2. Global variables
Global variables refer to variables declared in the application that can be shared by multiple pages. In UniApp, we can save the data that needs to be cached in global variables so that multiple pages can share this data. The sample code is as follows:
export default { globalData: { username: 'Tom' }, onLaunch() { // ... } }
var app = getApp(); console.log(app.globalData.username); // 输出:Tom
It should be noted that when using global variables, you need to obtain the App instance first and access its globalData property.
To sum up, this article introduces two ways of data caching in UniApp: local storage and global variables. Through these two methods, we can easily store and obtain data in the application. I hope this article will help you with data caching in UniApp development.
Reference materials:
The above is the detailed content of How to cache data in uniapp. For more information, please follow other related articles on the PHP Chinese website!