vue__WEBPACK_IMPORTED_MODULE_0__.defineComponent) is not a function

问题描述

再写vue项目时使用了,vue-router 插件,但是运行之后报错vue__WEBPACK_IMPORTED_MODULE_0__.defineComponent) is not a function,并且页面没有任何内容显示。

问题排查

根据报错内容,可以发现时一个defineCompenont函数没有定义,但是这显然不是我自己写的代码,所以应该是某个依赖的版本出现了问题,于是搜索发现是因为,我当前的package.json中配置的vue的版本是2.x而这个
defineCompenont是vue3.x的语法,于是问题就变得简单了,只需要修改package.json到合适的版本并再次下载即可。

npm install -S

然后根据官方文档,从vue2到vue3还需要安装 @vue/compiler-sfc

$ npm install -D @vue/compiler-sfc

再将这些文件调到合适的版本就可以了。以下是我目前的package.json

{
  "name": "demo",
  "version": "0.1.0",
  "private": true,
  "scripts": {
    "serve": "vue-cli-service serve",
    "build": "vue-cli-service build",
    "lint": "vue-cli-service lint"
  },
  "dependencies": {
    "@vue/shared": "^3.2.7",
    "core-js": "^3.6.5",
    "element-plus": "^1.1.0-beta.8",
    "qrcode.vue": "^1.7.0",
    "vue": "^3.0.0"
  },
  "devDependencies": {
    "@babel/core": "^7.12.16",
    "@babel/eslint-parser": "^7.12.16",
    "@vue/cli-plugin-babel": "^4.5.0",
    "@vue/cli-plugin-eslint": "^4.5.0",
    "@vue/cli-service": "^4.5.0",
    "@vue/compiler-sfc": "^3.0.0",
    "eslint": "^7.20.0",
    "eslint-plugin-vue": "^7.6.0",
    "node-sass": "^6.0.1",
    "sass-loader": "^12.1.0",
    "vue-router": "^4.0.0-beta.13"
  },
  "eslintConfig": {
    "root": true,
    "env": {
      "node": true
    },
    "extends": [
      "plugin:vue/essential",
      "eslint:recommended"
    ],
    "parserOptions": {
      "parser": "@babel/eslint-parser"
    },
    "rules": {}
  },
  "browserslist": [
    "> 1%",
    "last 2 versions",
    "not dead"
  ]
}

你可能感兴趣的:(vue__WEBPACK_IMPORTED_MODULE_0__.defineComponent) is not a function)