色综合图-色综合图片-色综合图片二区150p-色综合图区-玖玖国产精品视频-玖玖香蕉视频

您的位置:首頁技術(shù)文章
文章詳情頁

詳解vue3中組件的非兼容變更

瀏覽:3日期:2022-10-04 11:39:08
函數(shù)式組件

functional attribute 在單文件組件 (SFC) <template> 已被移除{ functional: true } 選項在通過函數(shù)創(chuàng)建組件已被移除

// 使用 <dynamic-heading> 組件,負(fù)責(zé)提供適當(dāng)?shù)臉?biāo)題 (即:h1,h2,h3,等等),在 2.x 中,這可能是作為單個文件組件編寫的:// 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)建的,換句話說,不需要定義 { functional: true } 組件選項。他們將接收兩個參數(shù):props 和 context。context 參數(shù)是一個對象,包含組件的 attrs,slots,和 emit property。此外,現(xiàn)在不是在 render 函數(shù)中隱式提供 h,而是全局導(dǎo)入 h。使用前面提到的 <dynamic-heading> 組件的示例,下面是它現(xiàn)在的樣子。

// vue3.0import { 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 的一部分傳遞,可以將其刪除異步組件的寫法與defineAsyncComponent方法

現(xiàn)在使用defineAsyncComponent助手方法,用于顯示的定義異步組件component選項重命名為loaderLoader函數(shù)本身不再接受resolve和rejuct參數(shù),必須返回一個Promise

// vue2.x// 以前異步組件是通過將組件定義為返回Promise的函數(shù)來創(chuàng)建const asyncPage = () => import(’./NextPage.vue’)// 或者以選項方式創(chuàng)建const asyncPage = { component: () => import(’./NextPage.vue’), delay: 200, timeout: 3000, error: ErrorComponent, loading: LoadingComponent}// vue3.x在vue3.x中,需要使用defineAsyncComponent來定義import{ defineAsyncComponent } from ’vue’import ErrorComponent from ’./components/ErrorComponent.vue’import LoadingComponent from ’./components/LoadingComponent.vue’// 不帶選項的定義方法const asyncPage = defineAsyncComponent(() => import(’./NextPage.vue’))// 帶選項的異步組件constasyncPageWithOptions = defineAsyncCopmonent({ loader: () => import(’./NextPage.vue’), delay: 200, timeout: 3000, errorComponent: ErrorComponent, LoadingComponent: LoadingComponent})

loader函數(shù)不再接收resolve和reject參數(shù),且必須始終返回Promise

// vue2.xconst oldAsyncComponent = (resolve, reject) => {}// vue3.xconst asyncComponent = defineAsyncComponent(() => new Promise((resolve, reject) => {}))組件事件需要在emits選項中聲明

vue3中現(xiàn)在提供了一個emits選項,類似props選項此選項可以用于定義組件向其父對象發(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來定義組件發(fā)出的事件 --><!-- 這個選項還接收已給對象,用來向props一樣對傳遞的參數(shù)進(jìn)行驗證 --><!-- 強(qiáng)烈建議記錄下每個組件發(fā)出的所有emits,因為去掉了.native修飾符,未使用聲明的事件的所有監(jiān)聽器都將包含在組建的$attr中,默認(rèn)情況下,該監(jiān)聽器將綁定到組件的根節(jié)點 --><template> <div> <p>{{ text }}</p> <button v-on:click='$emit(’accepted’)'>OK</button> </div></template><script> export default { props: [’text’], emits: [’accepted’] }</script>

以上就是詳解vue3中組件的非兼容變更的詳細(xì)內(nèi)容,更多關(guān)于vue3中組件的非兼容變更的資料請關(guān)注好吧啦網(wǎng)其它相關(guān)文章!

標(biāo)簽: Vue
相關(guān)文章:
主站蜘蛛池模板: 三及毛片 | 免费看a级| 韩国三级 mp4 | 视频在线色 | 成人亚洲欧美日韩在线 | heyzo国产亚洲高清 | 国产综合亚洲专区在线 | 欧美人在线 | 国产三级a三级三级午夜 | 寡妇野外啪啪一区二区 | 日韩在线观看视频免费 | 在线免费看黄色 | 欧美黄a | 成年人网站免费在线观看 | 成人影院欧美大片免费看 | 日本在线观看网址 | 国产精品成人一区二区三区 | 亚洲国产成人久久综合区 | 色妇女影院 | 欧洲一级片 | 欧美日韩亚洲精品一区 | 日本高清色www | 久草视频官网 | 欧美国产成人免费观看永久视频 | 午夜精品免费 | 欧美成人性色生活片免费在线观看 | 亚洲男人的天堂久久精品 | 欧美性色黄大片一级毛片视频 | 亚洲欧美一区二区三区国产精品 | 久久在线播放 | 国产91精品高清一区二区三区 | 热久久伊人 | 国产4tube在线播放 | 亚洲成人黄色网址 | 亚洲欧美日韩久久精品第一区 | 米奇777第四久久久99 | 亚洲专区视频 | 久久久久久综合一区中文字幕 | 日韩高清不卡在线 | 大桥未久在线精品视频在线 | 国产精品自在自线亚洲 |