Skip to content

浏览器存储

Storage

函数名描述类型参数返回值
Storage-class--
Storage.config配置(options:object)=>objectoptions:{type:类型,expired:过期时间,prefix:前缀}object
Storage.save保存(key: string, value: any, opts:object<options>)=>voidkey:标识符,value:保存的值, opts: 配置的参数-
Storage.get获取(key: string, opts:object<options>)=>stringkey:标识符, opts: 配置的参数string
Storage.remove移除(key: string, opts:object<options>)=>stringkey:标识符, opts: 配置的参数-
Storage.clear移除所有(opts:object<options>)=>voidopts: 配置的参数-

示例

在控制台查看效果

<template>
  <div>
    <XAction label="保存按钮" type="primary" @click="onSave"> </XAction>
    <XAction label="获取按钮" type="primary" @click="onGet"> </XAction>
    <XAction label="移除按钮" type="primary" @click="onRemove"> </XAction>
    <XAction label="移除所有按钮" type="primary" @click="onClear"> </XAction>
  </div>
</template>

<script setup lang="ts">
  import { XAction } from '@vtj/ui';

  import { Storage } from '@vtj/utils';

  const storage = new Storage({
    type: 'local', // session
    expired: 0,
    prefix: '__VTJ_'
  });

  const onSave = () => {
    storage.save('test', '测试保存');
  };

  const onGet = () => {
    console.log(storage.get('test'));
  };

  const onRemove = () => {
    storage.remove('test');
  };

  const onClear = () => {
    storage.clear();
  };
</script>

<style scoped></style>

Cookies

函数名描述类型参数返回值
set设置值(name:string,value:string,opts:object) => voidname: 键名,value: 保存的值,opts:选项-
get获取值(name:string) => stringname: 键名string
remove移除值(name:string,opts:object) => voidname: 键名,opts:选项-

opts 选项 object

  • expires?: number;
  • path?: string;
  • domain?: string;
  • secure?: boolean;
  • sameSite?: string;

示例

在控制台查看效果

<template>
  <div>
    <XAction label="set" type="primary" @click="onCookieSet"> </XAction>
    <XAction label="get" type="primary" @click="onCookieGet"> </XAction>
    <XAction label="remover" type="primary" @click="onCookieRemove"> </XAction>
  </div>
</template>

<script setup lang="ts">
  import { XAction } from '@vtj/ui';
  import { cookie } from '@vtj/utils';

  const onCookieSet = () => {
    cookie.set('cookie-test', 'cookie-test-测试');
  };

  const onCookieGet = () => {
    const getValue = cookie.get('cookie-test');
    console.log('onCookieGet', getValue);
  };

  const onCookieRemove = () => {
    cookie.remove('cookie-test');
  };
</script>

<style scoped></style>

Released under the MIT License.