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

vue-use-forward-ref

v1.0.1

Published

A lightweight Vue 3 composable to forward component ref and merge child exposed API onto parent instance

Readme

vue-use-forward-ref

npm Unit Test codecov size

English | 中文

轻量级 Vue 3 组合式 API,用于转发组件 ref,并将子组件的暴露 API 合并到父实例上。父级 ref 会同时暴露子组件的 expose() 以及可选的额外字段,统一在一处访问。

特性

  • 轻量 – 零依赖,体积小
  • 类型安全 – 完整 TypeScript 支持
  • 简单 APIuseForwardRef(ext?) 返回 { forwardRef },传给子组件的 ref 即可
  • 与 ext 合并 – 可选地将额外字段合并到父级暴露对象上

安装

# npm
npm install vue-use-forward-ref

# yarn
yarn add vue-use-forward-ref

# pnpm
pnpm add vue-use-forward-ref

# bun
bun add vue-use-forward-ref

基本用法

将子组件 ref 转发给父级

forwardRef 作为子组件的 ref 使用。父级(例如祖父组件)的 ref 会收到子组件的暴露 API。

<!-- Wrapper.vue -->
<script setup lang="ts">
import { ref } from 'vue';
import { useForwardRef } from 'vue-use-forward-ref';
import Child from './Child.vue';

const { forwardRef } = useForwardRef();
</script>

<template>
  <Child :ref="forwardRef" />
</template>
<!-- App.vue -->
<script setup lang="ts">
import type Child from './Child.vue';

const wrapperRef = useTemplateRef<InstanceType<typeof Child>>('wrapperRef');
</script>

<template>
  <Wrapper ref="wrapperRef" />
</template>

合并额外字段

可以将额外字段合并到暴露对象上,使父级 ref 既能访问子组件的 API,也能访问你传入的额外数据。

// types.ts
import type { Ref } from 'vue';
import type Child from './Child.vue';

export type WrapperExpose = {
  count: Ref<number>;
  reset: () => void;
};
export type WrapperInstance = InstanceType<typeof Child> & WrapperExpose;
<!-- Wrapper.vue -->
<script setup lang="ts">
import { ref } from 'vue';
import { useForwardRef } from 'vue-use-forward-ref';
import Child from './Child.vue';
import type { WrapperExpose } from './types';

const count = ref(0);
const { forwardRef } = useForwardRef<WrapperExpose>({
  count,
  reset: () => {
    count.value = 0;
  }
});
</script>

<template>
  <Child :ref="forwardRef" />
</template>
<!-- App.vue -->
<script setup lang="ts">
import type { WrapperInstance } from './types';
const wrapperRef = useTemplateRef<WrapperInstance>('wrapperRef');
</script>

<template>
  <Wrapper ref="wrapperRef" />
</template>

许可证

MIT

Copyright (c) 2026-present REFINIST