代码:
AsyncStorage封装
import {AsyncStorage} from "react-native"; class DeviceStorage { //保存数据 static saveData(keyName,keyValue) { var p = new Promise(function(resolve, reject){ //做一些异步操作 AsyncStorage.setItem(keyName, keyValue, function (error) { if (error) { return resolve('存储失败'); } else { return resolve('存储成功'); } }) }); return p; } static loadData(keyName) { var p = new Promise(function(resolve, reject){ //做一些异步操作 AsyncStorage.getItem(keyName, function (error, result) { if (!error) { return resolve(result); }else{ return resolve( '数据已经删除') } }) }); return p; } //删除指定的数据 static delData(keyName) { var p = new Promise(function (resolve,reject) { AsyncStorage.removeItem(keyName, function (error) { if (!error) { return resolve( '数据已经删除') ; }else{ return resolve('数据删除失败' ); } }) }) return p; } } export default DeviceStorage;
使用:
//保存 DeviceStorage.saveData("name","lpp").then(function(data){ alert(data) }); //获取 DeviceStorage.loadData("name").then(function(data){ alert(data) }); //删除 DeviceStorage.delData("name").then(function(data){ alert(data) });