Vue3中安装Vuex失败

Gan*_*Hap 8 vuex vuejs3

我想安装 Vuex 但出现错误。

// error
npm ERR! code ERESOLVE
npm ERR! ERESOLVE unable to resolve dependency tree
npm ERR! 
npm ERR! While resolving: vuejs-v-3@0.1.0
npm ERR! Found: vue@3.1.2
npm ERR! node_modules/vue
npm ERR!   vue@"^3.1.2" from the root project
npm ERR! 
npm ERR! Could not resolve dependency:
npm ERR! peer vue@"^2.0.0" from vuex@3.6.2
npm ERR! node_modules/vuex
npm ERR!   vuex@"*" from the root project
npm ERR! 
npm ERR! Fix the upstream dependency conflict, or retry
npm ERR! this command with --force, or --legacy-peer-deps
npm ERR! to accept an incorrect (and potentially broken) dependency resolution.
npm ERR! 
npm ERR! See /home/hap/.npm/eresolve-report.txt for a full report.

npm ERR! A complete log of this run can be found in:
npm ERR!     /home/hap/.npm/_logs/2021-06-24T06_46_36_463Z-debug.log
Run Code Online (Sandbox Code Playgroud)
// package.json

    {
  "name": "vuejs-v-3",
  "version": "0.1.0",
  "private": true,
  "scripts": {
    "serve": "vue-cli-service serve",
    "build": "vue-cli-service build",
    "lint": "vue-cli-service lint"
  },
  "dependencies": {
    "@apollo/client": "^3.3.20",
    "axios": "^0.21.1",
    "bootstrap": "^5.0.2",
    "core-js": "^3.6.5",
    "graphql": "^15.5.1",
    "vue": "^3.1.2",
    "vue-router": "^4.0.10"
  },
  "devDependencies": {
    "@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",
    "babel-eslint": "^10.1.0",
    "eslint": "^6.7.2",
    "eslint-plugin-vue": "^7.0.0"
  },
  "eslintConfig": {
    "root": true,
    "env": {
      "node": true
    },
    "extends": [
      "plugin:vue/vue3-essential",
      "eslint:recommended"
    ],
    "parserOptions": {
      "parser": "babel-eslint"
    },
    "rules": {}
  },
  "browserslist": [
    "> 1%",
    "last 2 versions",
    "not dead"
  ]
}

Run Code Online (Sandbox Code Playgroud)

小智 13

使用 Vue3 时,请参考Vuex 4 文档来安装 Vuex 4

npm install vuex@next --save

当使用 Vue2 时,请参考Vuex 3 文档来安装 Vuex 3

npm install vuex --save


shi*_*g00 7

正如@Seegy所说,

  • Vue 3 使用 Vuex 4
  • Vue 2 使用 Vuex 3

截至 2022 年 2 月,

  • 如果您使用的是 Vue 2,请使用以下命令安装 Vuex 3
    • npm install vuex@3.4.0 --save
  • 如果您使用的是 Vue 3,请使用以下命令安装 Vuex 4
    • npm install vuex --save或者npm install vuex@next --save


小智 4

有同样的错误,只需尝试 npm install vuex@next --save 而不是npm install vuex --save,它会起作用