npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

pomelo-dictionary

v0.2.0

Published

data-dictionary for Vue3

Downloads

3

Readme

Pomelo-dictioinary

应用于 Vue3 框架下的数据字典管理模式

特点

  1. 模块化管理选项类的公共数据
  2. 支持本地定义静态数据 与 接口动态数据,动态数据在使用到时才触发初始化
  3. 支持 TS 定义各自的数据类型,且在调用时有清晰提示
  4. 数据持久化(localStorage or sessionStorage)
  5. 支持以用户ID区分

实例参数

modules 数据模块集合

modules: Record<{string, {
  options: Ref<any[]>; // 数据选项列表,需Ref类型
  handleInit?: () => void; // 初始化方法,选填
  handleClear?: () => void; // 清除数据方法,选填
}}>

persistence 持久化选项,选填

persistence: {
  storage: Storage; // 传入 localStorage 或 sessionStorage
  nameSpace: string; // 项目/模块名称
  userName?: string; // 当前用户名,选填。亦可待后续调用实例方法更新
}

实例属性

  • dict - 数据模块集合
  • setUser(userName: string) - 设置用户id
  • update(property: string, params?: any) - 主动触发该数据模块的初始化方法,第二参数为可选,将会传递给目标模块的 handleInit 方法。该方法返回handleInit 的 Promise。
  • clear(property: string) - 主动触发该数据模块的清除方法
  • clearAll() - 主动触发所有数据模块的清除方法
  • getProperty(property: string) - 获取目标数据,返回Promise。适用于赋予选项默认值的场景

示例

// Demo目录结构
src
├─helpers
│  └─dictionary
│     ├─index.ts
│     └─modules
│        ├─demo1.ts
│        ├─demo2.ts
│        └─...
├─view
├─router
└─App.vue
// @/helpers/dictionary/index.ts 引入各数据模块 & 初始化

import { createPomeloDictionary } from 'pomelo-dictionary';
import { Ultraman, Kamen_Rider } from './modules/demo1';
import { Demo2 } from './modules/demo2';

// dict 数据集合,方便直接调用
// dictInstance 数据字典实例,包含数据本身与各方法
export const { dict, dictInstance } = createPomeloDictionary({
  modules: { // 各数据模块
    Ultraman,
    Kamen_Rider,
    Demo2
  },
  persistence: { // 持久化选项,选填
    storage: window.localStorage, // 传入 localStorage 或 sessionStorage
    nameSpace: 'myProjectName', // 项目/模块名称
    userName: 'Kakarotto_001' // 当前用户名,选填。亦可待后续调用实例方法更新
  }
});
// @/helpers/dictionary/modules/demo1.ts 静态数据

import { ref } from 'vue';

export const Ultraman = {
  options: ref<{
    label: string;
    value: string;
    other: number;
  }[]>([
    { label: '迪迦', value: 'Tiga', other: '圆大古' },
    { label: '戴拿', value: '1', other: '飞鸟信' },
    { label: '盖亚', value: 'Gaia', other: '高山我梦' }
  ])
}

export const Kamen_Rider = {
  options: ref<{
    label: string;
    value: string;
    other: number;
  }[]>([
    { label: '黑日', value: 'BlackSun', name: '南光太郎' },
    { label: '空我', value: 'Kuuga', name: '五代雄介' },
    { label: '亚极陀', value: 'Agito', name: '津上翔一' }
  ])
}
// @/helpers/dictionary/modules/demo2.ts 动态数据

import { ref } from 'vue';
import { someRequestMethod } from '@/api';

export const Demo2 = {
  options: ref<{
    name: string;
    id: number;
  }[]>([]),
  handleInit: function() { // 注意需返回Promise
    return someRequestMethod({ ... })
      .then(res => {
        if (res.code === 200 && Array.isArray(res.data)) {
          this.options = res.data;
        } else {
          throw new Error(res.message);
        }
      })
      .catch(err => {
        this.options = [{ name: '全部', id: 14 }];
        console.error(err.message);
      })
  },
  handleClear: function() {
    this.options = [];
  }
}
// @/App.vue 使用数据
<template>
  <select v-model="selectValue">
    <option
      v-for="item in optionList"
      :key="item.id"
      :value="item.id"
    >
      {{ item.name }}
    </option>
  </select>
</template>

<script setup lang="ts">
import { ref } from 'vue';
import { dict, dictInstance } from '@/helpers/dictionary'; // 引入

dictInstance.setUser('Goku'); // 更新当前用户

const selectValue = ref('');
const optionList = dict.Demo2; // 赋值时触发接口请求,返回 Ref 类型,接口完成后自动更新

// 赋予默认值
dictInstance.getProperty('Demo2').then(res => {
  selectValue.value = res[0].id;
})
</script>