yujinpan / el-select-tree

ElementUI's el-select combined with el-tree.
https://yujinpan.github.io/el-select-tree/
MIT License
172 stars 56 forks source link

使用demo报错,但传值正常 #37

Closed JeasonLaung closed 1 year ago

JeasonLaung commented 2 years ago
<template>
  <el-select-tree
    width="120px"
    placeholder="请选择内容"
    :data="treeData"
    v-model="value"
  ></el-select-tree>
</template>

<script>
import ElSelectTree from 'el-select-tree';

export default {
  components: {
    ElSelectTree
  },
  data() {
    return {
      value: 2,
      treeData: [
        {
          value: 1,
          label: 'text1',
          children: [
            { value: 5, label: 'text5' },
            { value: 6, label: 'text6' }
          ]
        },
        { value: 2, label: 'text2' },
        { value: 3, label: 'text3' },
        { value: 4, label: 'text5' }
      ]
    };
  }
};
</script>

image

JeasonLaung commented 2 years ago

建议使用watch,把外部和内部的值处理区分

yujinpan commented 2 years ago

感谢反馈,尽快排查问题。

JeasonLaung commented 2 years ago

2.0.9问题仍未处理

yujinpan commented 2 years ago

这是 nuxt 项目吗?

JeasonLaung commented 2 years ago

不是,普通vue项目

{
  "name": "vue-admin-template",
  "version": "4.4.0",
  "description": "A vue admin template with Element UI & axios & iconfont & permission control & lint",
  "author": "Pan <panfree23@gmail.com>",
  "scripts": {
    "serve": "vue-cli-service serve",
    "dev": "vue-cli-service serve",
    "build:prod": "vue-cli-service build",
    "build:stage": "vue-cli-service build --mode staging",
    "preview": "node build/index.js --preview",
    "svgo": "svgo -f src/icons/svg --config=src/icons/svgo.yml",
    "lint": "eslint --ext .js,.vue src",
    "test:unit": "jest --clearCache && vue-cli-service test:unit",
    "test:ci": "npm run lint && npm run test:unit"
  },
  "dependencies": {
    "axios": "0.18.1",
    "clipboard": "^2.0.8",
    "core-js": "^3.20.2",
    "element-ui": "2.15.7",
    "normalize.css": "7.0.0",
    "nprogress": "0.2.0",
    "path-to-regexp": "2.4.0",
    "vue": "2.6.10",
    "vue-router": "3.0.6",
    "vuex": "3.1.0",
    "wangeditor": "^4.7.11"
  },
  "devDependencies": {
    "@vue/cli-plugin-babel": "4.4.4",
    "@vue/cli-plugin-eslint": "4.4.4",
    "@vue/cli-plugin-unit-jest": "4.4.4",
    "@vue/cli-service": "4.4.4",
    "@vue/test-utils": "1.0.0-beta.29",
    "autoprefixer": "9.5.1",
    "babel-eslint": "10.1.0",
    "babel-jest": "23.6.0",
    "babel-plugin-dynamic-import-node": "2.3.3",
    "chalk": "2.4.2",
    "connect": "3.6.6",
    "eslint": "6.7.2",
    "eslint-plugin-vue": "6.2.2",
    "html-webpack-plugin": "3.2.0",
    "mockjs": "1.0.1-beta3",
    "runjs": "4.3.2",
    "sass": "1.33.0",
    "sass-loader": "8.0.2",
    "script-ext-html-webpack-plugin": "2.1.3",
    "serve-static": "1.13.2",
    "svg-sprite-loader": "4.1.3",
    "svgo": "1.2.2",
    "vue-template-compiler": "2.6.10"
  },
  "browserslist": [
    "> 1%",
    "last 2 versions"
  ],
  "engines": {
    "node": ">=8.9",
    "npm": ">= 3.0.0"
  },
  "license": "MIT"
}
SihenZhang commented 2 years ago

vue 版本:2.6.12 element-ui 版本:2.15.6 el-select-tree 版本:2.0.11

在上述版本的环境中存在同样问题,且每次重新渲染都会触发,虽然只是 Warn,但输入几个表单项就轻轻松松刷几千行,还是有点烦恼

yujinpan commented 2 years ago

好的,尽快修复!

SihenZhang commented 2 years ago

您好,我最近在调查这个问题时找到了原因和可能的解决方案。

原因:

  1. vue 的依赖写在了 dependencies 里,这会导致如果它和项目的 vue 版本不一致,就会单独安装一个 vue。 https://github.com/yujinpan/el-select-tree/blob/0ef6f9aea88f930176c14a827f3bf71b4eee133c/package.json#L25-L33 例如,我的项目中,vue 的版本为 2.6.14,而 dependencies 中的 vue 版本为 ^2.6.10,它会匹配版本号为 2.x.x 的最新版本,即 2.7.10,与项目的 vue 版本不一致,因此会重新在 npm 包的 node_modules 里下载 2.7.10 版本的 vue,可以通过下方截图的 lock 文件来确定这一点 image image
  2. 组件使用的 vue 实例与项目使用的 vue 实例不同(因为组件的 npm 包内的 node_modules 有一个不同版本的 vue,组件使用了 npm 包内的 node_moduels 的 vue),产生了冲突,导致相关的报错,可以通过下方截图的 debug 堆栈来确定这一点 image image

可能的解决方案: 参考这篇文章,dependencies 依赖会导致安装时自动安装,而使用该组件时一定会安装 vue,因此可以仿照 element-ui,将其改为 devDependenciespeerDependencies,这样就不会重复安装 vue,应该能解决这个问题 http://jingsam.github.io/2016/11/01/peerDependencies-in-Vue-components.html

yujinpan commented 2 years ago

Thanks! Now vue and element-ui moved to peerDependencies in v2.0.13.