Skip to content

Commit

Permalink
feat: 记一次 vue2 项目升级
Browse files Browse the repository at this point in the history
  • Loading branch information
yonghzhang committed Mar 3, 2024
1 parent 8a468fa commit d5220e2
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
- [如何做前端权限控制](https://forever-z.cn/#/articles/topic/methods-of-auth-control.md)
- [记一次底部菜单扩展性优化](https://forever-z.cn/#/articles/minds/menu-expandability.md)
- [小程序打包体积优化](https://forever-z.cn/#/articles/topic/optimization-of-mp-size.md)
- [记一次 vue2 项目升级](https://forever-z.cn/#/articles/topic/vue2-upgrade-to-vite.md)

> #### 学习笔记
Expand Down
1 change: 1 addition & 0 deletions _sidebar.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
- [如何做前端权限控制](/articles/topic/methods-of-auth-control.md)
- [记一次底部菜单扩展性优化](/articles/minds/menu-expandability.md)
- [小程序打包体积优化](/articles/topic/optimization-of-mp-size.md)
- [记一次 vue2 项目升级](/articles/topic/vue2-upgrade-to-vite.md)

#### 学习笔记

Expand Down
63 changes: 63 additions & 0 deletions articles/topic/vue2-upgrade-to-vite.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
# 记一次 vue2 项目升级到至 vite-vue3-ts 的过程

## 升级 vite

* `npm` 安装 `vite` `typescript` `@vitejs/plugin-vue`
* `packages.json` 中调整 `"type": "module"``scripts` 脚本
* 新建 `vite.config.ts`
* `plugins` 加上 `@vitejs/plugin-vue`
* `define` 加上 `process.env` 环境变量注入
* `resolve.alias` 加上比如 `@/`
* `resolve.extensions` 兼容,使得 `import ./App` 可缺失 `.vue`
* `server.port` 保持 `8088`
* `server.proxy` 使用新写法
* `svg` 文件使用 `vite-svg-loader` 加入 `plugins`
* `fbx` 文件使用 `assetsInclude: ['**/*.fbx']`
* 业务组件 `Uploader` 中使用了 `jsx`,暂用 `el-upload` 代替
* 多入口,配置 `build.rollupOptions.input`
* 打包结果的目录结构与旧项目一致,修改 `build.rollupOptions.output`
* 所有 `require.context` 改为 `import.meta.glob`

## 升级 vue3

* 调整 `router``vuex``i18n` 的导出与注册写法
* 去掉所有组件内 `filters`,转为 `methods` 写法
* 全局 `filters`,转为组件内引入
* 去掉所有的 `import Vue from 'vue'`
* 所有 `Vue.prototype` 改为 `app.config.globalProperties`
* `template[v-for]``key` 只能在 `template`
*`slot="title"` 改为 `#title`,且 `#title` 只能在 `template`
*`slot-scope` 改为 `#default` 等,且 `#default="scope"` 只能在 `template`
* 去掉全部 `v-on="$listeners"`
* `.native` 修饰符可能效果已消失,暂无方案
*`beforeDestroy` 改为 `beforeUnmount`
*`destroyed` 改为 `unmounted`
*`mixin` 中的 `beforeRouteEnter` 改为 `mounted`
* 全局 `$set` 失效,尽量去除,临时用 `x[y] = [z]` 代替
* 全局 `$on``$emit``$off` 失效,自己实现 `eventBus` 注入到全局
*`$createElement` 改为 `import { h } from 'vue'`,以及其组件实例相关问题
* `router.beforeEach``return``next` 只能有一种

## 升级 element-plus

* 去掉 `element-ui`,更改其引用,包含初始化、样式地址、`i18n`
* 调整部分组件引用,比如把 `Message` 改为 `ElMessage`
* 部分组件名称变动,比如把 `el-submenu``el-sub-menu`
*`el-button[type=text]` 改为 `el-button[link]`
*`el-dialog[custom-class]` 改为 `el-dialog[class]`
*`[value]` 改为 `[model-value]`
* 去掉所有 `[size=middle]`,把 `[size=mini]` 改为 `[size=small]`
*`[class="el-icon-warning"]` 改为 `<warning />`
*`el-dialog[visible]` 改为 `el-dialog[model-value]`
*`el-radio-group[@input]` 改为 `el-radio-group[@change]`
*`el-popover[v-model]` 改为 `el-popover[v-model:visible]`
*`el-popover[value]` 改为 `el-popover[visible]`
*`el-popover ref``doClose` 改为 `hide`
* `el-upload ref``uploadFiles` 失效,改为使用 `@onSuccess` 返回的 `files` 入参
* `el-select ref``wrap dom` 改为使用 `.scrollbarRef.wrapRef`

## 其他改造
* 较多组件有问题,`$emit('input')` 改为 `$emit('update:modelValue')`
* `eslint` 问题,更新最新写法
* 把所有 `/deep/` 改为 `:deep()`
*`vuex` 改为 `pinia`

0 comments on commit d5220e2

Please sign in to comment.