You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Vue3组件库导入其他项目时报错:Missing ref owner context 求助

Vue3组件库导入其他项目时报错:Missing ref owner context 求助

我目前正在用 Vite + TypeScript 开发一个 Vue3 组件库,但把这个库导入到另一个 Vue3 项目中使用时,弹出了如下报错:

runtime-core.esm.js:38 [Vue warn]: Missing ref owner context. ref cannot be used on hoisted vnodes. A vnode with ref must be created inside the render function.

查了相关资料后,我了解到这个问题一般是多个 Vue 实例被打包到一起导致的,但我已经在 vite.config.ts 里配置了 external: ["vue"],实在搞不懂为啥还会触发这个问题...

下面是我的 vite.config.ts 配置代码:

import { defineConfig } from "vite";
import vue from "@vitejs/plugin-vue";
import typescript2 from "rollup-plugin-typescript2";
import { resolve } from "path";
import cssInjectedByJsPlugin from "vite-plugin-css-injected-by-js";

// https://vitejs.dev/config/
export default defineConfig({
    plugins: [
        vue(),
        typescript2({
            check: false,
            include: ["src/**/*.vue", "src/**/*.ts"],
            tsconfigOverride: {
                compilerOptions: {
                    outDir: "dist",
                    sourceMap: true,
                    declaration: true,
                    declarationMap: true
                }
            },
            exclude: ["vite.config.ts"]
        }),
        cssInjectedByJsPlugin()
    ],
    build: {
        cssCodeSplit: false,
        sourcemap: false,
        lib: {
            entry: "./src/main.ts",
            formats: ["es", "cjs"],
            name: "RuntimeCore",
            fileName: format => `runtime-core.${format}.js`
        },
        rollupOptions: {
            external: ["vue"],
            output: {
                exports: "named",
                globals: {
                    vue: "Vue"
                }
            }
        }
    },
    resolve: {
        alias: {
            "@": resolve(__dirname, "src")
        }
    }
});

我是用这条命令来构建组件库并生成类型声明的:

vite build && vue-tsc --emitDeclarationOnly

我的 package.json 里的相关配置项如下:

"main": "dist/tk3d_hotspots.cjs.js",
 "module": "dist/tk3d_hotspots.es.js",
 "typings": "dist/main.d.ts",
 "type": "module",

有没有大佬也遇到过 Vue3 + Vite 组件库的这个问题呀?求指点!

备注:内容来源于stack exchange,提问作者claudegia

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.13 18:39:29