storage.uvue 4.7 KB
Newer Older
Y
init  
yurj26 已提交
1 2 3 4 5
<template>
    <view>
        <page-head :title="title"></page-head>
        <view class="uni-common-mt">
            <view class="uni-list">
Y
yurj26 已提交
6
                <view class="uni-list-cell uni-list-cell-line">
Y
init  
yurj26 已提交
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127
                    <view class="uni-list-cell-left">
                        <view class="uni-label">key</view>
                    </view>
                    <view class="uni-list-cell-db">
                        <input class="uni-input" type="text" placeholder="请输入key" name="key" :value="key"
                            @input="keyChange" />
                    </view>
                </view>
                <view class="uni-list-cell">
                    <view class="uni-list-cell-left">
                        <view class="uni-label">value</view>
                    </view>
                    <view class="uni-list-cell-db">
                        <input class="uni-input" type="text" placeholder="请输入value" name="data" :value="data"
                            @input="dataChange" />
                    </view>
                </view>
            </view>
            <view class="uni-padding-wrap">
                <view class="uni-btn-v">
                    <button type="primary" class="uni-btn btn-setstorage" @tap="setStorage">存储数据</button>
                    <button class="uni-btn" @tap="getStorage">读取数据</button>
                    <button class="uni-btn" @tap="clearStorage">清理数据</button>
                </view>
            </view>
        </view>
    </view>
</template>
<script lang="ts">
    export default {
        data() {
            return {
                title: 'get/set/clearStorage',
                key: '',
                data: ''
            }
        },
        methods: {
            keyChange: function (e : InputEvent) {
                this.key = e.detail.value
            },
            dataChange: function (e : InputEvent) {
                this.data = e.detail.value
            },
            getStorage: function () {
                var key = this.key;
                if (key.length == 0) {
                    uni.showModal({
                        title: '读取数据失败',
                        content: "key 不能为空",
                        showCancel: false
                    })
                } else {
                    uni.getStorage({
                        key: key,
                        success: (res) => {
                            uni.showModal({
                                title: '读取数据成功',
                                content: "data: '" + res.data + "'",
                                showCancel: false
                            })
                        },
                        fail: () => {
                            uni.showModal({
                                title: '读取数据失败',
                                content: "找不到 key 对应的数据",
                                showCancel: false
                            })
                        }
                    })
                }
            },
            setStorage: function () {
                var key = this.key
                var data = this.data
                if (key.length == 0) {
                    uni.showModal({
                        title: '保存数据失败',
                        content: 'key 不能为空',
                        showCancel: false
                    })
                } else {
                    uni.setStorage({
                        key: key,
                        data: data,
                        success: () => {
                            uni.showModal({
                                title: '存储数据成功',
                                content: data,
                                showCancel: false
                            })
                        },
                        fail: () => {
                            uni.showModal({
                                title: '储存数据失败!',
                                showCancel: false
                            })
                        }
                    })
                }
            },
            clearStorage: function () {
                this.key = ''
                this.data = ''
                uni.clearStorageSync()
                uni.showModal({
                    title: '清除数据成功',
                    content: ' ',
                    showCancel: false
                })
            }
        }
    }
</script>

<style>
    .btn-setstorage {
        background-color: #007aff;
        color: #ffffff;
    }
</style>