這篇文章主要介紹“如何變更vue3中組件的非兼容”,在日常操作中,相信很多人在如何變更vue3中組件的非兼容問(wèn)題上存在疑惑,小編查閱了各式資料,整理出簡(jiǎn)單好用的操作方法,希望對(duì)大家解答”如何變更vue3中組件的非兼容”的疑惑有所幫助!接下來(lái),請(qǐng)跟著小編一起來(lái)學(xué)習(xí)吧!
functional attribute 在單文件組件 (SFC) <template> 已被移除
{ functional: true } 選項(xiàng)在通過(guò)函數(shù)創(chuàng)建組件已被移除
// 使用 <dynamic-heading> 組件,負(fù)責(zé)提供適當(dāng)?shù)臉?biāo)題 (即:h2,h3,h4,等等),在 2.x 中,這可能是作為單個(gè)文件組件編寫的: // Vue 2 函數(shù)式組件示例 export default { functional: true, props: ['level'], render(h, { props, data, children }) { return h(`h${props.level}`, data, children) } } // Vue 2 函數(shù)式組件示例使用 <template> <template functional> <component :is="`h${props.level}`" v-bind="attrs" v-on="listeners" /> </template> <script> export default { props: ['level'] } </script>
現(xiàn)在在 Vue 3 中,所有的函數(shù)式組件都是用普通函數(shù)創(chuàng)建的,換句話說(shuō),不需要定義 { functional: true } 組件選項(xiàng)。
他們將接收兩個(gè)參數(shù):props 和 context。context 參數(shù)是一個(gè)對(duì)象,包含組件的 attrs,slots,和 emit property。
此外,現(xiàn)在不是在 render 函數(shù)中隱式提供 h,而是全局導(dǎo)入 h。
使用前面提到的 <dynamic-heading> 組件的示例,下面是它現(xiàn)在的樣子。
// vue3.0 import { h } from 'vue' const DynamicHeading = (props, context) => { return h(`h${props.level}`, context.attrs, context.slots) } DynamicHeading.props = ['level'] export default DynamicHeading // vue3.0單文件寫法 <template> <component v-bind:is="`h${$props.level}`" v-bind="$attrs" /> </template> <script> export default { props: ['level'] } </script>
主要區(qū)別在于
functional attribute 在 <template> 中移除 listeners 現(xiàn)在作為 $attrs 的一部分傳遞,可以將其刪除
現(xiàn)在使用defineAsyncComponent助手方法,用于顯示的定義異步組件
component選項(xiàng)重命名為loader
Loader函數(shù)本身不再接受resolve和rejuct參數(shù),必須返回一個(gè)Promise
// vue2.x // 以前異步組件是通過(guò)將組件定義為返回Promise的函數(shù)來(lái)創(chuàng)建 const asyncPage = () => import('./NextPage.vue') // 或者以選項(xiàng)方式創(chuàng)建 const asyncPage = { component: () => import('./NextPage.vue'), delay: 200, timeout: 3000, error: ErrorComponent, loading: LoadingComponent } // vue3.x 在vue3.x中,需要使用defineAsyncComponent來(lái)定義 import{ defineAsyncComponent } from 'vue' import ErrorComponent from './components/ErrorComponent.vue' import LoadingComponent from './components/LoadingComponent.vue' // 不帶選項(xiàng)的定義方法 const asyncPage = defineAsyncComponent(() => import('./NextPage.vue')) // 帶選項(xiàng)的異步組件 constasyncPageWithOptions = defineAsyncCopmonent({ loader: () => import('./NextPage.vue'), delay: 200, timeout: 3000, errorComponent: ErrorComponent, LoadingComponent: LoadingComponent })
loader函數(shù)不再接收resolve和reject參數(shù),且必須始終返回Promise
// vue2.x const oldAsyncComponent = (resolve, reject) => {} // vue3.x const asyncComponent = defineAsyncComponent(() => new Promise((resolve, reject) => {}))
vue3中現(xiàn)在提供了一個(gè)emits選項(xiàng),類似props選項(xiàng)
此選項(xiàng)可以用于定義組件向其父對(duì)象發(fā)出的事件
<!-- vue2.x --> <template> <div> <p>{{ text }}</p> <button v-on:click="$emit('accepted')">OK</button> </div> </template> <script> export default { props: ['text'] } </script> <!-- vue3.x --> <!-- 現(xiàn)在和prop類似,可以用emits來(lái)定義組件發(fā)出的事件 --> <!-- 這個(gè)選項(xiàng)還接收已給對(duì)象,用來(lái)向props一樣對(duì)傳遞的參數(shù)進(jìn)行驗(yàn)證 --> <!-- 強(qiáng)烈建議記錄下每個(gè)組件發(fā)出的所有emits,因?yàn)槿サ袅?native修飾符,未使用聲明的事件的所有監(jiān)聽器都將包含在組建的$attr中,默認(rèn)情況下,該監(jiān)聽器將綁定到組件的根節(jié)點(diǎn) --> <template> <div> <p>{{ text }}</p> <button v-on:click="$emit('accepted')">OK</button> </div> </template> <script> export default { props: ['text'], emits: ['accepted'] } </script>
到此,關(guān)于“如何變更vue3中組件的非兼容”的學(xué)習(xí)就結(jié)束了,希望能夠解決大家的疑惑。理論與實(shí)踐的搭配能更好的幫助大家學(xué)習(xí),快去試試吧!若想繼續(xù)學(xué)習(xí)更多相關(guān)知識(shí),請(qǐng)繼續(xù)關(guān)注創(chuàng)新互聯(lián)網(wǎng)站,小編會(huì)繼續(xù)努力為大家?guī)?lái)更多實(shí)用的文章!
分享名稱:如何變更vue3中組件的非兼容-創(chuàng)新互聯(lián)
本文鏈接:http://jinyejixie.com/article20/dphojo.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)頁(yè)設(shè)計(jì)公司、自適應(yīng)網(wǎng)站、外貿(mào)建站、營(yíng)銷型網(wǎng)站建設(shè)、小程序開發(fā)、移動(dòng)網(wǎng)站建設(shè)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容