亚洲精品久久久中文字幕-亚洲精品久久片久久-亚洲精品久久青草-亚洲精品久久婷婷爱久久婷婷-亚洲精品久久午夜香蕉

您的位置:首頁技術文章
文章詳情頁

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

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

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

// 使用 <dynamic-heading> 組件,負責提供適當的標題 (即:h1,h2,h3,等等),在 2.x 中,這可能是作為單個文件組件編寫的:// Vue 2 函數式組件示例export default { functional: true, props: [’level’], render(h, { props, data, children }) { return h(`h${props.level}`, data, children) }}// Vue 2 函數式組件示例使用 <template><template functional> <component :is='`h${props.level}`' v-bind='attrs' v-on='listeners' /></template><script>export default { props: [’level’]}</script>

現在在 Vue 3 中,所有的函數式組件都是用普通函數創建的,換句話說,不需要定義 { functional: true } 組件選項。他們將接收兩個參數:props 和 context。context 參數是一個對象,包含組件的 attrs,slots,和 emit property。此外,現在不是在 render 函數中隱式提供 h,而是全局導入 h。使用前面提到的 <dynamic-heading> 組件的示例,下面是它現在的樣子。

// 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>

主要區別在于

functional attribute 在 <template> 中移除listeners 現在作為 $attrs 的一部分傳遞,可以將其刪除異步組件的寫法與defineAsyncComponent方法

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

// vue2.x// 以前異步組件是通過將組件定義為返回Promise的函數來創建const asyncPage = () => import(’./NextPage.vue’)// 或者以選項方式創建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函數不再接收resolve和reject參數,且必須始終返回Promise

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

vue3中現在提供了一個emits選項,類似props選項此選項可以用于定義組件向其父對象發出的事件

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

以上就是詳解vue3中組件的非兼容變更的詳細內容,更多關于vue3中組件的非兼容變更的資料請關注好吧啦網其它相關文章!

標簽: Vue
相關文章:
主站蜘蛛池模板: 美女黄色免费 | 午夜亚洲国产成人不卡在线 | 午夜在线免费视频 | 播播网手机在线播放 | 日本亚洲精品久久 | 国产专区一区 | 2020国产成人精品免费视频 | 精品日产1区2区 | 国产日本特黄特色大片免费视频 | 深夜福利国产精品亚洲尤物 | 亚洲www.| 孕妇孕妇aaaaa级毛片视频 | 国产视频1区 | 精品免费久久久久国产一区 | 免费 欧美 自拍 在线观看 | 片成年免费观看网站黄 | 亚洲精品永久www嫩草 | 亚洲国产精品成人午夜在线观看 | 国产成人在线免费观看 | 免费黄色的视频 | 成人国产永久福利看片 | 婷婷射 | 国产三级毛片视频 | 午夜成年 | 亚洲一区二区三区网站 | 上海麻豆文化传媒网站入口 | 在线观看国产三级 | baoyu122.永久免费视频 | 免费国产高清视频 | 国产第二页| 免费人成xvideoscom中文 | 最近手机中文字幕无吗 | 久久久国产99久久国产首页 | 亚洲欧美手机在线观看 | 午夜国产福利视频一区 | 亚洲日韩男人网在线 | 亚洲欧美在线观看 | 亚洲国产欧美国产第一区二区三区 | 一区二区三区国模大胆 | 扒开双腿猛进入jk校视频 | 国产大学生自拍视频 |