詳解vue3中渲染函數(shù)的非兼容變更
渲染函數(shù)API變更
此更改不會影響到<template>用戶
- h現(xiàn)在全局導(dǎo)入,而非作為參數(shù)傳遞給渲染函數(shù)
- 渲染函數(shù)參數(shù)更改為在有狀態(tài)組件和函數(shù)組件之間更加一致
- vnode現(xiàn)在又一個扁平的prop結(jié)構(gòu)
Render函數(shù)參數(shù)
// 2.0 渲染函數(shù) export default { render(h) { return h('div') } } // 3.x語法 export default { render() { return h('div') } }
渲染函數(shù)簽名更改
// 2.x export default { render(h) { return h('div') } } // 3.x import { h, reactive } from 'vue' export default { setup(prop, {slots, attrs, emit}) { const state = reactive({ count: 0 }) function increment() { state.count++ } // 返回render函數(shù) return () => h( 'div', { onClick: increment }, state.count ) } }
VNode Props 格式化
// 2.x { class: ['button', 'is-outlined'], style: {color: '#fffff'}, attr: {id: 'submit'}, domProps: {innerHTML: ''}, on: {click: submitForm}, key: 'submit-button' } // 3.x VNode的結(jié)構(gòu)是扁平的 { class: ['button', 'is-outlined'], style: { color: '#34495E' }, id: 'submit', innerHTML: '', onClick: submitForm, key: 'submit-button' }
slot統(tǒng)一
更改了普通slot和作用域slot
- this.$slots現(xiàn)在將slots作為函數(shù)公開
- 移除this.$scopedSlots
// 2.x h(LayoutComponent, [ h('div', {slot: 'header'}, this.header), h('div', {slot: 'header'}, this.header) ]) // 作用域slot: // 3.x h(LayoutComponent, {}, { header: () => h('div', this.header), content: () => h('div', this.content) }) // 需要以編程方式引入作用域slot時,他們現(xiàn)在被統(tǒng)一在了$slots選項中 // 2.x的作用域slot this.$scopedSlots.header // 3.x的寫法 this.$slots.header
移除$listeners
$listeners
對象在vue3中已經(jīng)移除,現(xiàn)在事件監(jiān)聽器是$attrs
的一部分
在vue2中,可以使用this.attrs和this.attrs和this.listeners分別訪問傳遞給組件的attribute和時間監(jiān)聽器,結(jié)合inheritAttrs: false,開發(fā)者可以將這些attribute和監(jiān)聽器應(yīng)用到其他元素,而不是根元素
<template> <label> <input type="text" v-bind="$attrs" v-on="$listeners"> </label> </template> <script> export default { inheritAttrs: false } </script>
在vue的虛擬DOM中,事件監(jiān)聽器現(xiàn)在只是以on為前綴的attribute,這樣就成了attrs對象的一部分,這樣attrs對象的一部分,這樣listeners就被移除了
<template> <label> <input type="text" v-bind="$attrs" /> </label> </template> <script> export default { inheritAttrs: false } // 如果這個組件接收一個 id attribute 和一個 v-on:close 監(jiān)聽器,那么 $attrs 對象現(xiàn)在將如下所示 { id: 'my-input', onClose: () => console.log('close Event Triggered') } </script>
$attrs現(xiàn)在包括class和style
現(xiàn)在的$attr包含所有的attribute,包括class和style
在2.x中,虛擬dom會對class和style進行特殊處理,所以他們不包括在$attr中
在使用inheritAttr: false的時候會產(chǎn)生副作用
- $attrs 中的 attribute 不再自動添加到根元素中,而是由開發(fā)者決定在哪添加。
- 但是 class 和 style 不屬于 $attrs,仍然會應(yīng)用到組件的根元素:
<template> <label> <input type="text" v-bind="$attrs" /> </label> </template> <script> export default { inheritAttrs: false } </script> <!-- 寫入 --> <my-component id="my-id" class="my-class"></my-component> <!-- vue2 將生成 --> <label class="my-class"> <input type="text" id="my-id" /> </label> <!-- vue3 將生成 --> <label> <input type="text" id="my-id" class="my-class" /> </label>
以上就是詳解vue3中渲染函數(shù)的非兼容變更的詳細內(nèi)容,更多關(guān)于vue 渲染函數(shù)非兼容變更的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
vue項目使用.env文件配置全局環(huán)境變量的方法
這篇文章主要介紹了vue項目使用.env文件配置全局環(huán)境變量的方法,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-10-10vue3中使用ant-design-vue的layout組件實現(xiàn)動態(tài)導(dǎo)航欄和面包屑功能
這篇文章主要介紹了vue3中使用ant-design-vue的layout組件實現(xiàn)動態(tài)導(dǎo)航欄和面包屑功能,基于一個新建的Vue3項目上實現(xiàn),本文結(jié)合示例代碼給大家介紹的非常詳細,需要的朋友可以參考下2023-01-01Vue生產(chǎn)環(huán)境如何自動屏蔽console
這篇文章主要介紹了Vue生產(chǎn)環(huán)境如何自動屏蔽console問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-06-06Vue.js 2.0窺探之Virtual DOM到底是什么?
大家可能聽說Vue.js 2.0已經(jīng)發(fā)布,并且在其中新添加如了一些新功能。其中一個功能就是“Virtual DOM”。那么下面這篇文章就來給大家詳細介紹Vue.js 2.0中的Virtual DOM到底是什么?需要的朋友可以參考借鑒,下面來一起看看吧。2017-02-02Vue子組件關(guān)閉后調(diào)用刷新父組件的實現(xiàn)
這篇文章主要介紹了Vue子組件關(guān)閉后調(diào)用刷新父組件的實現(xiàn)方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-03-03