codecamp

百度智能小程序API 数据存储

setStorage


解释: 将数据存储在本地缓存指定的 key 中。如果之前存在同名 key ,会覆盖掉原来该 key 对应的内容。这是一个异步接口。

参数: Object

Object参数说明:

参数 类型 必填 说明
key String 本地缓存中的指定的 key
data Object/String 需要存储的内容
success Function 接口调用成功的回调函数
fail Function 接口调用失败的回调函数
complete Function 接口调用结束的回调函数(调用成功、失败都会执行)

success返回参数说明:

参数 类型 说明
status Number 状态码
message String 状态信息

示例:

swan.setStorage({
    key: 'mydata',
    data: 'abcdefg',
    success: function (res) {
        console.log(res.message);
    },
    fail: function (err) {
        console.log(“错误码:” + err.errCode);
        console.log(“错误信息:” + err.errMsg);
    }
});

setStorageSync


解释: 将数据存储在本地缓存中指定的 key 中。如果之前存在同名 key ,会覆盖掉原来该 key 对应的内容。这是一个同步接口。

参数: key, data

参数说明:

参数 类型 必填 说明
key String 本地缓存中的指定的 key
data Object/String 需要存储的内容

示例:

try {
    swan.setStorageSync('mydata', 'abcdefg');
} catch (e) {
}

getStorage


解释: 从本地缓存中异步获取指定 key 对应的内容。

参数: Object

Object参数说明:

参数 类型 必填 说明
key String 本地缓存中的指定的 key
success Function 接口调用成功的回调函数,res = {data: key对应的内容}
fail Function 接口调用失败的回调函数
complete Function 接口调用结束的回调函数(调用成功、失败都会执行)

success返回参数说明:

参数 类型 说明
data String key 对应的内容

示例:

swan.getStorage({
    key: 'mydata',
    success: function (res) {
        console.log(res.data);
    },
    fail: function (err) {
        console.log(“错误码:” + err.errCode);
        console.log(“错误信息:” + err.errMsg);
    }
});

getStorageSync


解释: 从本地缓存中同步获取指定 key 对应的内容。

参数: key

参数说明:

参数 类型 必填 说明
key String 本地缓存中的指定的 key

示例:

try {
    var result = swan.getStorageSync('mydata');
} catch (e) {
}

getStorageInfo


解释: 异步获取当前 storage 的相关信息。

参数: Object

Object参数说明:

参数 类型 必填 说明
success Function 接口调用成功的回调函数,详见返回参数说明
fail Function 接口调用失败的回调函数
complete Function 接口调用结束的回调函数(调用成功、失败都会执行)

success返回参数说明:

参数 类型 说明
keys String Array 当前 storage 中所有的 key
currentSize Number 当前占用的空间大小, 单位 kB
limitSize Number 限制的空间大小,单位 kB
state String 成功状态码
message String 成功信息

示例:

swan.getStorageInfo({
    success: function (res) {
        console.log(res.keys);
    },
    fail: function (err) {
        console.log("错误码:" + err.errCode);
        console.log("错误信息:" + err.errMsg);
    }
});

getStorageInfoSync


解释: 同步获取当前 storage 的相关信息。

示例:

try {
    var result = swan.getStorageInfoSync();
    console.log(result);
} catch (e) {
}

removeStorage


解释: 从本地缓存中异步移除指定 key。

参数: Object

Object参数说明:

参数 类型 必填 说明
key String 本地缓存中的指定的 key
success Function 接口调用成功的回调函数
fail Function 接口调用失败的回调函数
complete Function 接口调用结束的回调函数(调用成功、失败都会执行)

示例:

swan.removeStorage({
    key: 'mydata',
    success: function (res) {
        console.log(res);
	},
    fail: function (err) {
        console.log("错误码:" + err.errCode);
        console.log("错误信息:" + err.errMsg);
    }
});

removeStorageSync


解释: 从本地缓存中同步移除指定 key。

参数: key

参数说明:

参数 类型 必填 说明
key String 本地缓存中的指定的 key

示例:

try {
    swan.removeStorageSync('mydata');
} catch (e) {
}

clearStorage


解释: 清理本地数据缓存。

Object参数说明:

参数 类型 必填 说明
success Function 接口调用成功的回调函数
fail Function 接口调用失败的回调函数
complete Function 接口调用结束的回调函数(调用成功、失败都会执行)

success返回参数说明:

参数 类型 说明
state String 成功状态码
message String 成功信息

示例:

swan.clearStorage({
    success: function (res) {
        console.log(res.state);
        console.log(res.message);
	},
    fail: function (err) {
        console.log("错误码:" + err.errCode);
        console.log("错误信息:" + err.errMsg);
    }
});

clearStorageSync


解释: 同步清理本地数据缓存。

示例:

try {
    swan.clearStorageSync();
} catch(e) {
}
百度智能小程序API 文件
百度智能小程序API 位置
温馨提示
下载编程狮App,免费阅读超1000+编程语言教程
取消
确定
目录

百度智能小程序开发文档

百度智能小程序 组件

百度智能小程序 地图

百度智能小程序 画布

百度智能小程序 API

百度智能小程序 界面

百度智能小程序 关注小程序引导组件

百度智能小程序 自定义组件

百度智能小程序 媒体

百度智能小程序 设备

百度智能小程序 拨打电话

百度智能小程序 内存警报

百度智能小程序 手机联系人

百度智能小程序 用户截屏事件

百度智能小程序 第三方平台

百度智能小程序 开放接口

百度智能小程序 百度收银支付

百度智能小程序 分包预下载

百度智能小程序 数据分析

百度智能小程序 服务端

百度智能小程序 云开发

百度智能小程序 初始化

百度智能小程序 云函数

百度智能小程序 服务端初始化

百度智能小程序 服务器获取上下文

百度智能小程序 服务端云函数

百度智能小程序 开发教程

百度智能小程序 功能开发

百度智能小程序 基本原理

百度智能小程序 小程序自动化

百度智能小程序 视频教程

关闭

MIP.setData({ 'pageTheme' : getCookie('pageTheme') || {'day':true, 'night':false}, 'pageFontSize' : getCookie('pageFontSize') || 20 }); MIP.watch('pageTheme', function(newValue){ setCookie('pageTheme', JSON.stringify(newValue)) }); MIP.watch('pageFontSize', function(newValue){ setCookie('pageFontSize', newValue) }); function setCookie(name, value){ var days = 1; var exp = new Date(); exp.setTime(exp.getTime() + days*24*60*60*1000); document.cookie = name + '=' + value + ';expires=' + exp.toUTCString(); } function getCookie(name){ var reg = new RegExp('(^| )' + name + '=([^;]*)(;|$)'); return document.cookie.match(reg) ? JSON.parse(document.cookie.match(reg)[2]) : null; }