108 lines
2.3 KiB
Vue
108 lines
2.3 KiB
Vue
<template>
|
|
<div class="demo">
|
|
<div class="bar">
|
|
<input v-model="key" class="field" placeholder="键" />
|
|
<input v-model="value" class="field" placeholder="值" />
|
|
<button type="button" class="btn" @click="save" v-text="'写入'" />
|
|
<button type="button" class="btn" @click="load" v-text="'读取'" />
|
|
<button type="button" class="btn" @click="remove" v-text="'删除'" />
|
|
</div>
|
|
|
|
<p class="state" v-text="`当前键存在:${exists}`" />
|
|
|
|
<ol class="log">
|
|
<li v-for="(item, index) in log" :key="index" v-text="item" />
|
|
</ol>
|
|
</div>
|
|
</template>
|
|
|
|
<script lang="ts" setup>
|
|
import { computed, ref } from "vue";
|
|
import { Storage } from "timi-web";
|
|
|
|
const key = ref("timi-demo");
|
|
const value = ref("hello storage");
|
|
const log = ref<string[]>([]);
|
|
/** 手动维护一个计数,用来在增删后重新读取 has */
|
|
const version = ref(0);
|
|
|
|
/** 直接读真实 localStorage,演示里的增删会反映到浏览器里。 */
|
|
const exists = computed(() => {
|
|
void version.value;
|
|
return Storage.has(key.value);
|
|
});
|
|
|
|
function push(message: string) {
|
|
log.value = [`${new Date().toLocaleTimeString()} ${message}`, ...log.value].slice(0, 6);
|
|
version.value += 1;
|
|
}
|
|
|
|
function save() {
|
|
Storage.setObject(key.value, value.value);
|
|
push(`setObject("${key.value}", "${value.value}")`);
|
|
}
|
|
|
|
function load() {
|
|
const raw = Storage.getString(key.value);
|
|
push(`getString → ${raw === null ? "null" : raw}`);
|
|
}
|
|
|
|
function remove() {
|
|
Storage.remove(key.value);
|
|
push(`remove("${key.value}")`);
|
|
}
|
|
</script>
|
|
|
|
<style lang="less" scoped>
|
|
.demo {
|
|
.bar {
|
|
display: flex;
|
|
flex-wrap: wrap;
|
|
gap: .5rem;
|
|
margin-bottom: 1rem;
|
|
}
|
|
|
|
.field {
|
|
padding: .25rem .5rem;
|
|
font-size: .8125rem;
|
|
border: 1px solid var(--vp-c-divider);
|
|
border-radius: .375rem;
|
|
color: var(--vp-c-text-1);
|
|
background: var(--vp-c-bg);
|
|
}
|
|
|
|
.btn {
|
|
padding: .25rem .75rem;
|
|
font-size: .75rem;
|
|
cursor: pointer;
|
|
border: 1px solid var(--vp-c-divider);
|
|
border-radius: .375rem;
|
|
color: var(--vp-c-text-1);
|
|
background: var(--vp-c-bg-soft);
|
|
|
|
&:hover {
|
|
color: var(--vp-c-brand-1);
|
|
border-color: var(--vp-c-brand-1);
|
|
}
|
|
}
|
|
|
|
.state {
|
|
margin: 0 0 .75rem;
|
|
font-size: .8125rem;
|
|
color: var(--vp-c-text-2);
|
|
}
|
|
|
|
.log {
|
|
margin: 0;
|
|
padding-left: 1.25rem;
|
|
font-size: .75rem;
|
|
font-family: monospace;
|
|
color: var(--vp-c-text-2);
|
|
|
|
li {
|
|
margin-bottom: .25rem;
|
|
}
|
|
}
|
|
}
|
|
</style>
|