-
Notifications
You must be signed in to change notification settings - Fork 273
OpenTiny Vue2 升级 Vue3 操作手册
安装 Vue CLI @vue/cli 5.0.8
npm install -g @vue/cli
创建 Vue2 项目
vue create opentiny-vue2-to-vue3
输出以下信息说明项目创建成功
Successfully created project opentiny-vue2-to-vue3.
Get started with the following commands:
cd opentiny-vue2-to-vue3
npm run serve
cd opentiny-vue2-to-vue3
npm run serve
输出以下日志说明启动成功
App running at:
- Local: http://localhost:8080/
- Network: http://192.168.1.102:8080/
效果如下
npm i vue-router@3 @opentiny/vue@2
修改package.json
文件中的 vue / vue-template-compiler 版本号前面的 ^
,删除 package-lock.json 文件,执行下 npm i
import Vue from 'vue'
import App from './App.vue'
import VueRouter from 'vue-router'
const router = new VueRouter({
routes: [
{
path: '/',
component: () => import('./components/HomePage.vue')
},
{
path: '/form',
component: () => import('./components/FormPage.vue')
},
{
path: '/list',
component: () => import('./components/ListPage.vue')
}
]
})
Vue.config.productionTip = false
Vue.use(VueRouter)
new Vue({
router,
render: h => h(App),
}).$mount('#app')
<template>
<div id="app">
<img alt="Vue logo" src="./assets/logo.png" />
Vue version: {{ version }}
<p>
<!-- use the router-link component for navigation. -->
<!-- specify the link by passing the `to` prop. -->
<!-- `<router-link>` will render an `<a>` tag with the correct `href` attribute -->
<router-link to="/">Home</router-link>
<router-link to="/form">Form</router-link>
<router-link to="/list">List</router-link>
</p>
<!-- route outlet -->
<!-- component matched by the route will render here -->
<router-view></router-view>
</div>
</template>
<script>
import * as Vue from 'vue'
export default {
name: 'App',
components: {},
data: function () {
return {
version: Vue.version,
}
},
}
</script>
<style>
#app {
font-family: Avenir, Helvetica, Arial, sans-serif;
-webkit-font-smoothing: antialiased;
-moz-osx-font-smoothing: grayscale;
text-align: center;
color: #2c3e50;
margin-top: 60px;
}
</style>
- src/components/HomePage.vue
- src/components/FormPage.vue
- src/components/ListPage.vue
<template>
<div>Hello OpenTiny</div>
</template>
打开链接:
https://opentiny.design/tiny-vue/zh-CN/os-theme/components/form
复制Form组件示例代码:
在FormPage.vue中粘贴复制好的代码。
https://opentiny.design/tiny-vue/zh-CN/os-theme/components/grid
复制Grid组件示例代码:
效果如下
参考链接:
• 表单:
https://opentiny.design/tiny-vue/zh-CN/os-theme/components/form
• 表格:
https://opentiny.design/tiny-vue/zh-CN/os-theme/components/grid
npm install gogocode-cli -g
查看 gogocode版本号:
gogocode --version
0.2.28
3.2 转换源码
gogocode -s ./src -t gogocode-plugin-vue -o ./src
中间有一个确认的步骤,需要输入 y,按Enter确认
转换完之后会遇到以下两个问题,需要做两处修改:
问题一:error 'v-model' directives require no argument vue/no-v-model-argument
解决方法:修改FormPage.vue中的v-model:value 为v-model即可
问题二:Failed to resolve component: router-link
解决方案:修改main.js中use(router)代码顺序即可
window.$vueApp = Vue.createApp(App)
window.$vueApp.use(router) // 这一行代码需要放到 mount 之前
window.$vueApp.mount('#app')
gogocode -s package.json -t gogocode-plugin-vue -o package.json
npm i @opentiny/vue@3
组件代码无需做任何修改,完成Vue2项目平滑升级到Vue3。