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 🙏

© 2026 – Pkg Stats / Ryan Hefner

@bams-app/global-data

v0.2.1

Published

globalData 业务组件

Readme

组件说明文档

包定位

@bams-app/ui-store 当前提供两部分能力:

  1. 一个默认导出的页面根组件 uiStore,用于承接通用容器和 work 配置。
  2. 一组基于 Vue 3 Composition API 的全局数据仓库方法,用于注册、加载、复用全局共享数据。

这个仓库通过 globalThis 挂载单例,在同一运行时内可被多个组件复用,避免同一份基础数据重复请求。

目录说明

ui-store/
├── index.js            # 默认导出组件入口
└── src/
    ├── component.vue   # 根组件
    ├── index.js        # 全局数据仓库 API
    └── register.js     # 内置全局数据注册

当前导出说明

1. 默认入口

包根入口 index.js 仅默认导出组件:

import GlobalData from "@bams-app/global-data";

2. Store API 入口

全局数据相关方法定义在 src/index.js,使用时需要按源码路径引入:

import {
  defineGlobalData,
  useGlobalData,
  ensureGlobalData,
  preloadGlobalData,
  clearGlobalData,
  getGlobalDataState,
} from "@bams-app/global-data/src/index.js";

3. 内置注册入口

内置基础数据定义在 src/register.js。该文件不会随着默认入口自动执行,如需使用内置数据项,需要在应用入口或页面初始化阶段手动引入一次:

import "@bams-app/global-data/src/register.js";

根组件说明

src/component.vue 本身不承载业务逻辑,主要作用是:

  1. 包裹统一容器 ComponentContainer
  2. 挂载页面主内容 MainContent
  3. 通过 defineOptions 预留 dicts 和 config 配置

如果只是使用全局数据仓库能力,可以不直接使用该根组件。

4. 定义静态数据

如果需要定义一些不需要异步加载的全局静态数据,可以在调用 defineGlobalData 时只提供 defaultValue 而不提供 loader:

import { defineGlobalData } from "@bams-app/global-data";

// 定义静态配置
defineGlobalData("APP_CONFIG", {
  defaultValue: {
    title: "BAMS 系统",
    theme: "dark",
  },
});

// 使用时,loaded 状态默认为 true
const { data } = useGlobalData("APP_CONFIG");
console.log(data.value.title); // 'BAMS 系统'

内置全局数据项

src/register.js 当前预置了以下全局数据:

| key | 默认值 | 自动加载 | 数据来源 | 说明 | | --------------- | ------ | -------- | ----------------------------------------------------- | ------------------------------------------ | | locationCode | [] | 是 | bdApi.getLocationCodeList() | 位置信息列表,组件挂载后会自动触发首次加载 | | dimensionList | [] | 否 | bdApi.getDimensionList({ current: 1, size: 10000 }) | 维度列表,需要手动调用加载 |

返回结果会经过 transform 处理:

locationCode => res?.records || res || [];
dimensionList => res?.records || [];

使用方式

方式一:直接使用内置全局数据

先注册内置数据:

import "@bams-app/ui-store/src/register.js";

在组件中使用:

<script setup>
  import { useGlobalData } from "@bams-app/ui-store/src/index.js";

  const { data: locationCode, loading, loaded, error, refresh } = useGlobalData("locationCode");
</script>

说明:

  1. locationCode 默认开启自动加载,组件挂载后会自动请求。
  2. refresh() 会强制重新拉取最新数据。
  3. data、loading、loaded、error 都是可直接使用的计算属性。

方式二:注册自定义全局数据

import { defineGlobalData } from "@bams-app/ui-store/src/index.js";
import { userApi } from "@/api/user";

defineGlobalData("userList", {
  defaultValue: [],
  autoLoad: false,
  loader: () => userApi.getList(),
  transform: res => res?.records || [],
});

然后在组件中读取:

<script setup>
  import { useGlobalData } from "@bams-app/ui-store/src/index.js";

  const {
    data: userList,
    load,
    refresh,
    clear,
  } = useGlobalData("userList", {
    autoLoad: false,
  });

  load();
</script>

适用场景:

  1. 下拉选项
  2. 字典映射
  3. 多页面共享的基础档案数据
  4. 请求成本高、适合缓存复用的数据

API 说明

defineGlobalData(key, definition)

注册一个全局数据定义。重复注册同一个 key 时,会直接返回已存在的状态对象,不会覆盖已有定义。

definition 支持以下字段:

| 字段 | 类型 | 必填 | 默认值 | 说明 | | -------------- | ---------- | ---- | ------------------ | ------------------------------ | | loader | Function | 是 | - | 实际请求函数 | | defaultValue | any | 否 | null | 初始值 | | autoLoad | boolean | 否 | true | useGlobalData 时是否自动加载 | | transform | Function | 否 | (value) => value | 请求结果转换函数 |

useGlobalData(key, options)

返回当前 key 对应的响应式访问能力:

| 返回字段 | 说明 | | ------------------- | ---------------- | | state | 只读状态对象 | | data | 当前数据 | | loading | 是否加载中 | | loaded | 是否已成功加载 | | error | 最近一次加载错误 | | load(loadOptions) | 加载数据 | | refresh() | 强制刷新 | | clear() | 清空当前缓存状态 |

options.autoLoad 可覆盖注册时的 autoLoad 设置。

ensureGlobalData(key, options)

确保数据已加载完成,返回 Promise:

await ensureGlobalData("dimensionList");
await ensureGlobalData("dimensionList", { force: true });

行为说明:

  1. 已加载且未强制刷新时,直接返回缓存数据。
  2. 正在请求中时,复用同一个 Promise,避免重复请求。
  3. force: true 时会忽略已加载状态,重新发起请求。

preloadGlobalData(keys)

批量预加载多个全局数据:

await preloadGlobalData(["locationCode", "dimensionList"]);

clearGlobalData(key)

清空指定 key 的缓存数据与状态。

getGlobalDataState(key)

直接获取底层状态对象。该方法更适合调试或框架层封装,业务组件通常优先使用 useGlobalData。

接入建议

推荐按下面顺序接入:

  1. 在应用入口统一引入 @bams-app/ui-store/src/register.js,完成内置数据注册。
  2. 对公共基础数据使用 defineGlobalData 统一定义加载规则。
  3. 业务组件内部通过 useGlobalData 消费数据,避免在多个页面重复请求。
  4. 对页面首屏依赖较强的数据,可在路由进入前或页面初始化时调用 preloadGlobalData。

注意事项

  1. useGlobalData、ensureGlobalData 等方法在调用前,必须先完成对应 key 的注册,否则会抛出 global data "xxx" is not defined 错误。
  2. clearGlobalData 会把数据重置为 null,而不是回退为注册时的 defaultValue,如果业务依赖空数组或空对象,需要自行兜底。
  3. 该仓库的单例挂载在 globalThis 上,适合同一前端运行时内共享数据;如果页面被完全销毁重建,缓存也会随运行时重置。