vue框架render方法如何替換template
render方法替換template
使用template屬性創(chuàng)建組件模板
import Vue from 'vue' ? const Item = Vue.component('Item', { ? template: `<div> ? ? ? ? ? ? ? ? <h2>子組件</h2> ? ? ? ? ? ? ? ? <slot></slot> ? ? ? ? ? ? ? </div>` }) const app = new Vue({ ? el: '#app', ? template: `<div ref="myDiv"> ? ? ? ? ? ? ?<item ref="item"> ? ? ? ? ? ? ? ? <p ref="p">this is a slot</p> ? ? ? ? ? ? ? </item> ? ? ? ? ? ? </div>`, ?data: { ? ? count: 0 ?}, ? components: { ? ? Item ? } })
把父組件的template創(chuàng)建換成使用render方法創(chuàng)建
const app = new Vue({ ? el: '#app', ? data: { ? ? count: 0 ? }, ? render (createElement) { ? ? return createElement( ? ? ? 'div', { ? ? ? ? ref: 'myDiv', ? ? ? ? // domProps: { ? ? ? ? // ? ?innerHTML: '<span>注意:添加該屬性會(huì)把后面的子節(jié)點(diǎn)覆蓋</span>' ? ? ? ? // }, ? ? ? ? attrs: { ? ? ? ? ? ? id: 'test-id', ? ? ? ? ? ? title: 'test-title' ? ? ? ? ? } ? ? ? }, ? ? ? [ ? ? ? ? createElement('item', { ? ? ? ? ? ref: 'item' ? ? ? ? }, ? ? ? ? [ ? ? ? ? ? createElement('p', { ? ? ? ? ? ? ref: 'p' ? ? ? ? ? }, 'this is a slot') ? ? ? ? ]) ? ? ? ]) ? }, ? components: { ? ? Item ? } })
1.如上面更改后的代碼,render方法內(nèi)傳入createElement函數(shù),接下來使用createElement函數(shù)來創(chuàng)建節(jié)點(diǎn)。
2.函數(shù)方法格式 createElement('節(jié)點(diǎn)或組件名稱', {節(jié)點(diǎn)屬性}, [子節(jié)點(diǎn)])
3.先創(chuàng)建一個(gè)div元素, 內(nèi)部包含ref='myDiv'的屬性, 使用數(shù)組存放其子節(jié)點(diǎn)
4.數(shù)組內(nèi)子節(jié)點(diǎn)是 item組件, 屬性是 ref="item", 其子節(jié)點(diǎn)為p, 依次類推逐層創(chuàng)建子節(jié)點(diǎn), 最后的文本節(jié)點(diǎn)使用字符串或變量即可,無需再用[]包含。
template和render用法對(duì)比
App.vue(主入口文件)
<template> ? ? <ParentCmp /> </template>
<script> import ParentCmp from './ParentCmp'; export default { ? ? components: { ? ? ? ? ParentCmp ? ? }, } </script>
ParentCmp.vue (template寫法)
<template> ? ? <div> ? ? ? ? <h1>我是parent組件</h1> ? ? ? ? <hr /> ? ? ? ? <User style="background: #ccc" text="我是傳入的文本"> ? ? ? ? ? ? <template v-slot:header> ? ? ? ? ? ? ? ? <p>這是名字為header的slot</p> ? ? ? ? ? ? </template> ? ? ? ? ? ? <p>這是填充默認(rèn)slot數(shù)據(jù)</p> ? ? ? ? ? ? <template v-slot:footer> ? ? ? ? ? ? ? ? <p>這是名字為footer的slot</p> ? ? ? ? ? ? </template> ? ? ? ? ? ? <template v-slot:item="props"> ? ? ? ? ? ? ? ? <p>名字為item的作用域插槽。顯示數(shù)據(jù){{props}}</p> ? ? ? ? ? ? </template> ? ? ? ? ? ? <template v-slot:list="props"> ? ? ? ? ? ? ? ? <p>名字為list的作用域插槽。顯示數(shù)據(jù){{props}}</p> ? ? ? ? ? ? </template> ? ? ? ? </User> ? ? </div> </template>
<script> import User from './User' export default { ? ? components: { ? ? ? ? User ? ? }, ? ? props: {}, ? ? data() { ? ? ? ? return {} ? ? }, ? ? methods: {} } </script>
User.vue (template寫法)
<template> ? ? <div> ? ? ? ? <h4>{{text}}</h4> ? ? ? ? <slot name="header"></slot> ? ? ? ? <slot>默認(rèn)的user slot</slot> ? ? ? ? <slot name="footer"></slot> ? ? ? ? <slot name="item" v-bind="item">item作用域插槽,展示姓名 {{item.name}}</slot> ? ? ? ? <slot name="list" v-bind="{list}">list作用域插槽</slot> ? ? </div> </template>
<script> export default { ? ? props: { ? ? ? ? text: String ? ? }, ? ? data() { ? ? ? ? return { ? ? ? ? ? ? item: { ? ? ? ? ? ? ? ? name: '張三', ? ? ? ? ? ? ? ? age: 28, ? ? ? ? ? ? ? ? works: '前端、后端、設(shè)計(jì)、產(chǎn)品' ? ? ? ? ? ? }, ? ? ? ? ? ? list: ['a','b','c'] ? ? ? ? } ? ? } } </script>
ParentCmp.js (render寫法)
import User from './User' export default { ? ? props: {}, ? ? data() { ? ? ? ? return {} ? ? }, ? ? methods: {}, ? ? render(h) { ? ? ? ? return h('div',[ ? ? ? ? ? ? h('h1', '我是parent組件'), ? ? ? ? ? ? h('hr'), ? ? ? ? ? ? h(User, { ? ? ? ? ? ? ? ? props: { ? ? ? ? ? ? ? ? ? ? text: '我是傳入的文本' ? ? ? ? ? ? ? ? }, ? ? ? ? ? ? ? ? style: { ? ? ? ? ? ? ? ? ? ? background: '#ccc' ? ? ? ? ? ? ? ? }, ? ? ? ? ? ? ? ? // 作用域插槽寫在scopedSlots里 ? ? ? ? ? ? ? ? scopedSlots: { ? ? ? ? ? ? ? ? ? ? item: props => h('p', `名字為item的作用域插槽。顯示數(shù)據(jù)${JSON.stringify(props)}`), ? ? ? ? ? ? ? ? ? ? list: props => h('p', `名字為list的作用域插槽。顯示數(shù)據(jù)${JSON.stringify(props)}`) ? ? ? ? ? ? ? ? } ? ? ? ? ? ? },? ? ? ? ? ? ? // 非作用域插槽寫數(shù)組里 ? ? ? ? ? ? [ ? ? ? ? ? ? ? ? h('p', {slot: 'header'}, '這是名字為header的slot'), ? ? ? ? ? ? ? ? h('p', '這是填充默認(rèn)slot數(shù)據(jù)'), ? ? ? ? ? ? ? ? h('p', {slot: 'footer'}, '這是名字為footer的slot'), ? ? ? ? ? ? ]) ? ? ? ? ]); ? ? ? ? // jxs寫法 ? ? ? ? /* return ( ? ? ? ? ? ? <div> ? ? ? ? ? ? ? ? <h1>我是parent組件</h1> ? ? ? ? ? ? ? ? <hr /> ? ? ? ? ? ? ? ? <User? ? ? ? ? ? ? ? ? ? ? style="background: #ccc"? ? ? ? ? ? ? ? ? ? ? text="我是傳入的文本"? ? ? ? ? ? ? ? ? ? ? scopedSlots={ ? ? ? ? ? ? ? ? ? ? ? ? { ? ? ? ? ? ? ? ? ? ? ? ? ? ? item: props => (<p>名字為item的作用域插槽。顯示數(shù)據(jù){JSON.stringify(props)}</p>), ? ? ? ? ? ? ? ? ? ? ? ? ? ? list: props => (<p>名字為list的作用域插槽。顯示數(shù)據(jù){JSON.stringify(props)}</p>), ? ? ? ? ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? > ? ? ? ? ? ? ? ? ? ? <p slot="header">這是名字為header的slot</p> ? ? ? ? ? ? ? ? ? ? <p>這是填充默認(rèn)slot數(shù)據(jù)</p> ? ? ? ? ? ? ? ? ? ? <p slot="footer">這是名字為footer的slot</p> ? ? ? ? ? ? ? ? </User> ? ? ? ? ? ? </div> ? ? ? ? ); */ ? ? } }
User.js (render寫法)
export default { ? ? props: { ? ? ? ? text: String ? ? }, ? ? data () { ? ? ? ? return { ? ? ? ? ? ? item: { ? ? ? ? ? ? ? ? name: '張三', ? ? ? ? ? ? ? ? age: 28, ? ? ? ? ? ? ? ? works: '前端、后端、設(shè)計(jì)、產(chǎn)品' ? ? ? ? ? ? }, ? ? ? ? ? ? list: ['a', 'b', 'c'] ? ? ? ? } ? ? }, ? ? methods: { ? ? ? ? getSlot (name, data) { ? ? ? ? ? ? if (this.$scopedSlots[name]) { ? ? ? ? ? ? ? ? return this.$scopedSlots[name](data); ? ? ? ? ? ? } else if (this.$slots[name]) { ? ? ? ? ? ? ? ? return this.$slots[name]; ? ? ? ? ? ? } ? ?? ? ? ? ? ? ? return undefined; ? ? ? ? }, ? ? }, ? ? render (h) { ? ? ? ? return h('div', [ ? ? ? ? ? ? h('h4', this.text), ? ? ? ? ? ? this.getSlot('header'), ? ? ? ? ? ? this.$slots.default, ? ? ? ? ? ? this.getSlot('footer'), ? ? ? ? ? ? this.getSlot('item', this.item), ? ? ? ? ? ? this.getSlot('list', {list: this.list}), ? ? ? ? ]) ? ? ? ? // jxs寫法 ? ? ? ? /* return ( ? ? ? ? ? ? <div> ? ? ? ? ? ? ? ? <h4>{this.text}</h4> ? ? ? ? ? ? ? ? {this.getSlot('header')} ? ? ? ? ? ? ? ? {this.$slots.default} ? ? ? ? ? ? ? ? {this.getSlot('footer')} ? ? ? ? ? ? ? ? {this.getSlot('item', this.item)} ? ? ? ? ? ? ? ? {this.getSlot('list', {list: this.list})} ? ? ? ? ? ? </div> ? ? ? ? ); */ ? ? } }
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
vue3 學(xué)習(xí)筆記之a(chǎn)xios的使用變化總結(jié)
本篇文章主要旨在幫助正在學(xué)vue3或者準(zhǔn)備學(xué)vue3的同學(xué)了解網(wǎng)絡(luò)請(qǐng)求axios該如何使用,防止接觸了一點(diǎn)點(diǎn)vue3的同學(xué)會(huì)有個(gè)疑問。有興趣的小伙伴可以關(guān)注一下2021-11-11uniapp開發(fā)打包多端應(yīng)用完整方法指南
這篇文章主要介紹了uniapp開發(fā)打包多端應(yīng)用完整流程指南,包括了uniapp打包小程序,uniapp打包安卓apk,uniapp打包IOS應(yīng)用,需要的朋友可以參考下2022-12-12vue在路由中驗(yàn)證token是否存在的簡(jiǎn)單實(shí)現(xiàn)
今天小編就為大家分享一篇vue在路由中驗(yàn)證token是否存在的簡(jiǎn)單實(shí)現(xiàn),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2019-11-11詳解在Vue.js編寫更好的v-for循環(huán)的6種技巧
這篇文章主要介紹了詳解在Vue.js編寫更好的v-for循環(huán)的6種技巧,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-04-04在vue項(xiàng)目實(shí)現(xiàn)一個(gè)ctrl+f的搜索功能
剛剛接到領(lǐng)導(dǎo)通知,需要實(shí)現(xiàn)搜索功能,因?yàn)轫?xiàng)目是vue的而且是手機(jī)端,對(duì)我來說有點(diǎn)小難度。經(jīng)過小編的一番思索最終還是解決了,今天小編把實(shí)現(xiàn)過程分享到腳本之家平臺(tái),需要的朋友參考下2020-02-02vue cli使用融云實(shí)現(xiàn)聊天功能的實(shí)例代碼
最近小編接了一個(gè)新項(xiàng)目,項(xiàng)目需求要實(shí)現(xiàn)一個(gè)聊天功能,今天小編通過實(shí)例代碼給大家介紹vue cli使用融云實(shí)現(xiàn)聊天功能的方法,感興趣的朋友跟隨小編一起看看吧2019-04-04el-date-picker時(shí)間清空值為null處理方案
本文介紹關(guān)于Vue.js項(xiàng)目中時(shí)間選擇器組件的問題,當(dāng)選擇后清空導(dǎo)致值變?yōu)閚ull,進(jìn)而引發(fā)后臺(tái)接口報(bào)錯(cuò),通過監(jiān)聽`overallForm.time`的值并設(shè)置為空數(shù)組,成功解決此問題,確保了數(shù)據(jù)正確性,同時(shí),建議避免直接監(jiān)聽整個(gè)對(duì)象以優(yōu)化性能,感興趣的朋友一起看看吧2024-08-08VUE異步更新DOM - 用$nextTick解決DOM視圖的問題
這篇文章主要介紹了VUE異步更新DOM - 用$nextTick解決DOM視圖的問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-11-11