制作一个项目用于研究elementUI的源码

需求:修改el-tooltip的颜色,发现传递参数等方法都不太好用,也可以使用打断点的方式,但也有点麻烦,因此打算直接修改源码,把组件逻辑给修改了

第一步下载源码

源码地址

GitHub - ElemeFE/element: A Vue.js 2.0 UI Toolkit for Web

第二步,创建vue项目

使用vuecli创建vue项目,在src下面创建element-ui文件夹,把上面源码的packages文件夹和src文件夹放入elementui中

第三步,处理导入问题

把复制的源码中引入element-ui的源头指向本地的源码内容,需要修改vue.config.js文件

javascript 复制代码
const { defineConfig } = require('@vue/cli-service')
const path = require("path");
module.exports = defineConfig({
  configureWebpack: {
    resolve: {
      alias: {
        "@": path.resolve(__dirname, "src"),
         // "element-ui": "2.14.1",
        "element-ui": path.resolve(__dirname, "src/element-ui"),
      },
    },
  },
  transpileDependencies: true
})

第四步,在main.js中引入

由于上面修改了vue.config.js,这里使用的element-ui都是本地的内容

javascript 复制代码
import Vue from 'vue'
import App from './App.vue'
import ElementUI from 'element-ui/src/index';
import 'element-ui/packages/theme-chalk/src/index.scss';
Vue.config.productionTip = false
Vue.use(ElementUI)
new Vue({
  render: h => h(App),
}).$mount('#app')

第五步,解决报错

源码中未使用的变量等其他内容,在npm run serve的时候会报错,所以要关掉。在packages.json文件中的eslintConfig写入下面的rules,关掉提示

javascript 复制代码
"rules": {
      "no-console": "off",
      "no-debugger": "off",
      "no-extra-semi": "off",
      "no-unused-vars": "off",
      "no-undef": "off",
      "no-prototype-builtins": "off",
      "no-useless-escape": "off",
      "vue/multi-word-component-names": "off",
      "vue/no-mutating-props": "off",
      "no-empty": "off",
      "vue/no-unused-components": "off",
      "vue/no-side-effects-in-computed-properties": "off",
      "vue/no-use-v-if-with-v-for": "off",
      "no-case-declarations": "off",
      "vue/return-in-computed-property": "off",
      "vue/valid-v-model": "off"
    }

第六步,包的处理

源码中有些地方引入了包/依赖,需要安装一下才能使用源码,这里可以直接复制下面的内容,然后重新npm i

javascript 复制代码
"dependencies": {
    "async-validator": "~1.8.1",
    "core-js": "^3.8.3",
    "deepmerge": "^1.2.0",
    "normalize-wheel": "^1.0.1",
    "resize-observer-polyfill": "^1.5.0",
    "throttle-debounce": "^1.0.1",
    "vue": "^2.6.14"
  },
  "devDependencies": {
    "@babel/core": "^7.12.16",
    "@babel/eslint-parser": "^7.12.16",
    "@vue/cli-plugin-babel": "~5.0.0",
    "@vue/cli-plugin-eslint": "~5.0.0",
    "@vue/cli-service": "~5.0.0",
    "eslint": "^7.32.0",
    "eslint-plugin-vue": "^8.0.3",
    "sass": "^1.35.2",
    "sass-loader": "^10.1.1",
    "vue-template-compiler": "^2.6.14"
  },

之后直接使用组件的内容,而且还可以直接修改源码就能看到效果。便于研究源码内容和运行原理

相关推荐
炫饭第一名1 小时前
速通Canvas指北🦮——基础入门篇
前端·javascript·程序员
王晓枫1 小时前
flutter接入三方库运行报错:Error running pod install
前端·flutter
符方昊1 小时前
React 19 对比 React 16 新特性解析
前端·react.js
ssshooter1 小时前
又被 Safari 差异坑了:textContent 拿到的值居然没换行?
前端
曲折1 小时前
Cesium-气象要素PNG色斑图叠加
前端·cesium
Forever7_2 小时前
Electron 淘汰!新的桌面端框架 更强大、更轻量化
前端·vue.js
Angelial2 小时前
Vue3 嵌套路由 KeepAlive:动态缓存与反向配置方案
前端·vue.js
jiayu2 小时前
Angular学习笔记24:Angular 响应式表单 FormArray 与 FormGroup 相互嵌套
前端
jiayu2 小时前
Angular6学习笔记13:HTTP(3)
前端
小码哥_常2 小时前
Kotlin抽象类与接口:相爱相杀的编程“CP”
前端