uniapp 本地数据存储

1、设置
	同步
		uni.setStorageSync('获取值的id值',任意类型存储值);
		
	异步
		uni.setStorage({
			key:'获取值的id值',
			data:80,
			success(任意类型存储值){ 存储成功回调}
		})
		
2、获取
	同步
		var res=uni.getStorageSync('获取值的id值');
		
	异步
		uni.getStorage({
			key:'获取值的id值',
			success(res){ 成功获取回调
				console.log(res.data);
			}
		})
		
3、移除
	同步
		uni.removeStorageSync('获取值的id值');
	异步
		uni.removeStorage({
			key:'获取值的id值',
			success(){移除成功回调}
		})

代码示例:

<template>
	<view>
		<view class='box'>
			{
     {
     msg}}
		</view>
		<text class='iconfont icon-shipin '></text>
		<text class='iconfont'>&#xe650;</text>
		<text v-if="true" v-html="hhh">wwww</text>
		<button type="primary" @click="get">按钮</button>
		<button type='primary' @click='set'>按钮2</button>
		<button type='primary' @click='remv'>按钮3</button>
		<view v-for="(item,index) in list" :key="index">
			<view>{
     {
     item}}</view>
		</view>
	</view>
</template>

<script>
export default{
     
	data()
	{
     
		return{
     
			msg:'数据',
			hhh:'ww',
			list:[1,2,3,4,5,6,7,8]
		}
	},
	onReachBottom()
	{
     
		this.list.push(10);
		
	},
	methods:{
     
		get()
		{
     
			// uni.getStorage({
     
			// 	key:'id',
			// 	success(res){
     
			// 		console.log(res.data);
			// 	}
			// })
			
			var res=uni.getStorageSync('id');
			console.log(res);
		},
		set()
		{
     
			// uni.setStorage({
     
			// 	key:'id',
			// 	data:80,
			// 	success(){
     
			// 		console.log('存储成功')
			// 	}
			// })
			
			uni.setStorageSync('id',100);
			
		},
		remv()
		{
     
			// uni.removeStorage({
     
			// 	key:'id',
			// 	success()
			// 	{
     
			// 		console.log('移出成功');
			// 	}
			// })
			
			uni.removeStorageSync('id');
		}
	},

}
</script>

<style scoped>
	@import url("../css/a.css");
	.box{
     
		height: 1000rpx;
		width: 375rpx;
		background-color: #4CD964;
	}
	.box1{
     
		background-color: #007AFF;
	}
</style>

你可能感兴趣的:(uniapp,uniapp)