vue3升級(jí)常見(jiàn)問(wèn)題詳細(xì)匯總
Ⅰ、前言
雖然 vue3
是沒(méi)有刪除 vue2
的 選項(xiàng)式 API
, 但是我們升級(jí)vue3 還是需要修改很多問(wèn)題的
下面來(lái)看看我們升級(jí)常見(jiàn)的一些問(wèn)題 ??
Ⅱ、解決兼容問(wèn)題
1、路由的創(chuàng)建方式
① vue2
寫(xiě)法
const router = new VueRouter({ routes: [] }); export default router;
②改為 vue3
寫(xiě)法
import { createRouter, createWebHistory } from 'vue-router' const routerHistory = createWebHistory() const router = createRouter({ history: routerHistory, routes: [] }) export default router
2、路由的方法變化
this.$router.push({path: '/bbb', query: {username: "abc"}});
修改為
import { useRouter } from 'vue-router' const router = useRouter() router.push({ path:'/bbb', params:{ username: 'posva'} });
3、升級(jí) vuex 到 4.x
vue2 | vue3 |
---|---|
vue2要用vuex 3.x 版本 | vue3要用vuex 4.x 版本 |
4、作用域 插槽語(yǔ)法修改
在 2.6
以下
<template slot-scope="row"> <span>{{row.name}}</span> </template> ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ <template slot="header"> <span>666</span> </template>
2.6
以上及 3.x
則需要改為 ??
<template v-slot:default="row"> <span>{{row.name}}</span> </template> 或 <template #default="row"> <span>{{row.name}}</span> </template> ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ <template v-slot:header> <span>666</span> </template>
5、具名插槽不能重復(fù)
錯(cuò)誤寫(xiě)法 ??
<Comp> <span>999</span> <template #default> <span>666</span> </template> <template #default> <span>777</span> </template> </Comp>
正確寫(xiě)法 ??
<Comp> <template #default> <span>999</span> <span>666</span> <span>777</span> </template> </Comp>
6、根掛載的變化
import Vue from 'vue' import App from './App.vue' import router from './router' //路由 import store from './store' //vuex new Vue({ router, store, render: h => h(App) }).$mount('#app')
修改為 ??
import { createApp } from 'vue' import App from './App.vue' import router from './router' //路由 import store from './store' //vuex createApp(App) .use(store) .use(router) .mount('#app')
7、模板 v-for ,必須在模板上掛載 key
錯(cuò)誤寫(xiě)法 ??
<template v-for="item in list"> <div :key='item.key'>{{item.name}}</div> </template>
正確寫(xiě)法 ??
<template v-for="item in list" :key='item.key'> <div>{{item.name}}</div> </template>
8、遞歸組件 寫(xiě)法變化
如一個(gè)簡(jiǎn)化的tree
例子
<template> <Tree :list ="list"> </template> <script > import Tree from './Tree.vue' export default { data() { return { list:[ {name:'aaa' , children:[{ name:'ccc' }] } , {name:'bbb'} ] } } </script>
vue2
需要導(dǎo)入本身
<template> <div v-for='item in list' :key='item.name'> <span>{{item.name}}</span> <Tree :list ="list.children" v-if='list.children'> </div> </template> <script> import Tree from './Tree.vue' export default { components: { Tree }, } }; </script>
vue3
根據(jù)組件名
<template> <div v-for='item in list' :key='item.name'> <span>{{item.name}}</span> <Tree :list ="list.children" v-if='list.children'> </div> </template> <script> export default { name:'Tree' } </script>
9、深層樣式寫(xiě)法變化
如 :
::v-deep .input__text{<!--{C}%3C!%2D%2D%20%2D%2D%3E--> }
修改為:
:deep(.input__text){<!--{C}%3C!%2D%2D%20%2D%2D%3E--> }
可以利用 全局匹配修改
選擇正則匹配
::v-deep\s(.*)\s :deep($1)
10、生命周期鉤子函數(shù) 命名修改
beforeDestroy() => beforeUnmount() destroyed() => unmounted() 刪除 created() 生命周期
11、數(shù)據(jù)總線 eventBus 變化
vue3
中已經(jīng)移除了 eventBus
的一些方法 , 但是通過(guò)一點(diǎn)點(diǎn)代碼就能自己實(shí)現(xiàn)一個(gè)
查看詳情 => vue3 eventBus
12、異步組件
components:{ asyncCom1 :() => import('../components/test-com') }
vue3
則要 修改為 ??
import { defineAsyncComponent } from 'vue' const asyncCom2 = defineAsyncComponent(() => import('組件路徑'))
13、ui 組件庫(kù)
ui
組件庫(kù)的 ,則需要參照 ui
組件庫(kù)的文檔進(jìn)行修改
總結(jié)
到此這篇關(guān)于vue3升級(jí)常見(jiàn)問(wèn)題的文章就介紹到這了,更多相關(guān)vue3升級(jí)問(wèn)題內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
前端登錄退出處理Token問(wèn)題(獲取、緩存、失效處理)及代碼實(shí)現(xiàn)方法
token是一個(gè)用戶信息的表示,在登錄中將會(huì)從后端拿到token,然后用戶才可以進(jìn)行往后的一系列操作,這篇文章主要給大家介紹了關(guān)于前端登錄退出處理Token問(wèn)題(獲取、緩存、失效處理)及代碼實(shí)現(xiàn)的相關(guān)資料,需要的朋友可以參考下2024-01-01Vue 將后臺(tái)傳過(guò)來(lái)的帶html字段的字符串轉(zhuǎn)換為 HTML
這篇文章主要介紹了Vue 將后臺(tái)傳過(guò)來(lái)的帶html字段的字符串轉(zhuǎn)換為 HTML ,需要的朋友可以參考下2018-03-03element?table?表格控件實(shí)現(xiàn)單選功能
本文主要介紹了element?table?表格控件實(shí)現(xiàn)單選功能,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2022-07-07Vue3快速實(shí)現(xiàn)文件上傳OSS的方法詳解
這篇文章給大家介紹了Vue3快速實(shí)現(xiàn)文件上傳OSS的方法,上傳文件可以說(shuō)是經(jīng)典的需求了,在后臺(tái)管理項(xiàng)目中隨處可見(jiàn),一般是由前端進(jìn)行文件上傳,然后再由后端去處理,本文旨在實(shí)現(xiàn)上傳功能,不考慮額外的功能(如文件尺寸限制),感興趣的朋友可以參考下2024-01-01vue2.0中vue-cli實(shí)現(xiàn)全選、單選計(jì)算總價(jià)格的實(shí)例代碼
本篇文章主要介紹了vue2.0中vue-cli實(shí)現(xiàn)全選、單選計(jì)算總價(jià)格的實(shí)例代碼,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-07-07