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

Vue3迁移时出现‘vue is not defined’错误求助

Vue3迁移构建中vue is not defined错误的解决方法

你的问题出在同时启用了Vue3的@vitejs/plugin-vue和Vue2的@vitejs/plugin-vue2插件,这会导致Vite的Vue运行环境冲突,进而触发vue is not defined错误。迁移构建场景下,只需要用Vue3的插件配合@vue/compat兼容层即可,不需要注册Vue2的插件。

修正步骤:

  1. 移除@vitejs/plugin-vue2相关的导入和插件注册
  2. 确保正确导入并使用Vue3官方的@vitejs/plugin-vue插件
  3. 保留@vue/compat的别名映射和兼容模式配置

修正后的vite.config.js示例:

// vite.config.js
import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue' // 导入Vue3官方插件

export default defineConfig({
  resolve: {
    alias: {
      vue: '@vue/compat'
    }
  },
  plugins: [
    vue({
      template: {
        compilerOptions: {
          compatConfig: {
            MODE: 2 // 启用Vue2兼容模式
          }
        }
      }
    })
  ]
})

额外检查项:

  • 确保项目中所有Vue相关的导入都用import Vue from 'vue',别名会自动映射到@vue/compat
  • 避免直接使用全局的window.Vue,迁移构建要求通过ES模块导入的方式使用Vue

内容的提问来源于stack exchange,提问作者coure2011

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.25 06:47:11