Vue 學(xué)習(xí)筆記 render函數(shù)

Vue 學(xué)習(xí)筆記 九 、render函數(shù)

9.1 render函數(shù)初步了解

template下只允許有一個(gè)子節(jié)點(diǎn)

<div id="app">
            <child :level="level">
                klk
            </child>

        </div>
        
                    
<!--        <template id="hdom">
            <div>
            <h1 v-if="level==1">
                <slot></slot>
            </h1>               
            <h2 v-if="level==2">
                <slot></slot>
            </h2>               
            <h3 v-if="level==3">
                <slot></slot>
            </h3>
            </div>
        </template> -->
        <script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
        <script>
//使用vue組件定義
//          Vue.component('child', {
//              props:['level'],
//              template: '#hdom'
//          })

//使用render定義
Vue.component('child', {
    render: function(createElement){
        return createElement('h'+this.level,
         this.$slots.default);
    },
    props:['level']
})

            var app = new Vue({
                el: '#app',
                data: {level:3}
            })
        </script>

9.2 render函數(shù)的第一個(gè)參數(shù)

在render函數(shù)的方法中,參數(shù)必須是createElement,createElement的類型是function,render函數(shù)的第一個(gè)參數(shù)可以是 String | Object | Function

        <script>
Vue.component('child', {
            // ----第一個(gè)參數(shù)必選
            //String--html標(biāo)簽
            //Object---一個(gè)含有數(shù)據(jù)選項(xiàng)的對(duì)象
            //Function---方法返回含有數(shù)據(jù)選項(xiàng)的對(duì)象
            render: function (createElement) {
                alert(typeof createElement)
                                //return createElement('div')
                // return createElement('h1')
                // return createElement({
                // template:'<div>鋤禾日當(dāng)午</div>'
                // })
                var domFun = function () {
                    return {
                        template: '<div>鋤禾日當(dāng)午</div>'
                    }
                }
                return createElement(domFun());
            }
        });

            var app = new Vue({
                el: '#app',
                data: {level:3}
            })
        </script>

9.3 render函數(shù)的第二個(gè)參數(shù)

        <div id="app">

<child></child>
        </div>

        <script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
        <script>
            Vue.component('child', {
                // ----第二個(gè)參數(shù)可選,第二個(gè)參數(shù)是數(shù)據(jù)對(duì)象----只能是Object
                render: function(createElement) {
                    return createElement({
                        template: '<div>我是龍的傳人</div>'
                    }, {
                        'class': {
                            foo: true,
                            baz: false
                        },
                        style: {
                            color: 'red',
                            fontSize: '16px'
                        },
                        //正常的html特性
                        attrs: {
                            id: 'foo',
                            src: 'http://baidu.com'
                        },
                        //用來(lái)寫原生的Dom屬性
                        domProps: {
                            // innerHTML: '<span style="color:blue;font-size: 18px">我是藍(lán)色</span>'
                        }
                    })
                }
            });

            var app = new Vue({
                el: '#app',
                data: {
                    level: 3
                }
            })
        </script>

9.3 render函數(shù)的第三個(gè)參數(shù)

第三個(gè)參數(shù)也是可選===String | Array—作為我們構(gòu)建函數(shù)的子節(jié)點(diǎn)來(lái)使用的

Vue.component('child', {
            // ----第三個(gè)參數(shù)是可選的,可以是 String | Array---代表子節(jié)點(diǎn)
            render: function (createElement) {
                return createElement('div', [
                    createElement('h1', '我是h1標(biāo)題'),
                    createElement('h6', '我是h6標(biāo)題')
                ])
            }
        });

9.4 this.$slots在render函數(shù)中的應(yīng)用

createElement(‘header’,header), 返回的就是VNODE
var header = this.$slots.header? //–這返回的內(nèi)容就是含有=VNODE的數(shù)組

            Vue.component('child', {
                render: function(createElement) {
                    var header = this.$slots.header;
                    var main = this.$slots.default;
                    var footer = this.$slots.footer;
                    return createElement('div', [
                        createElement('header', header),
                        createElement('main', main),
                        createElement('footer', footer)
                    ]);
                }
            });

9.5 在render函數(shù)中使用props傳遞數(shù)據(jù)

            Vue.component('my-component', {
                props: ['show'],
                render: function(createElement) {
                    var imgsrc;
                    if (this.show) {
                        imgsrc = 'img/001.jpg'
                    } else {
                        imgsrc = 'img/002.jpg'
                    }
                    return createElement('img', {
                        attrs: {
                            src: imgsrc
                        },
                        style: {
                            width: '600px',
                            height: '400px'
                        }
                    });
                    }
                
            })
            var app = new Vue({
                el: '#app',
                data: {
                    show: false
                },
                methods: {
                    switchShow() {
                        this.show = !this.show;
                    }
                }
            })

9.6 v--model在render函數(shù)中的使用

        <div id="app">
                    <my-component :name="name" v-model="name"></my-component>
            <br> {{name}}
        </div>

        <script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
        <script>
            Vue.component('my-component', {
                props: ['name'],
                render: function(createElement) {
                    var self = this; //指的就是當(dāng)前的VUE實(shí)例
                    return createElement('input', {
                            domProps: {
                                value: self.name
                            },
                        on: {
                            input: function(event) {
                                //此處的this指的是什么?指的就是window
//                              var a = this;
//                              console.log(a)
                                console.log(self)
                                self.$emit('input', event.target.value)
                            }
                        }
                    })
                }       
            })
            
            var app=new Vue({
                el:'#app',
                data:{
                    name:'',
                }
            })

9.7 作用域插槽在render函數(shù)中的使用

            Vue.component('my-component', {
                render:function(creatElement){
                    return creatElement('div',this.$scopedSlots.default({
                        text:'傳遞的信息',
                        msg:'scopetext'
                    }))
                }
            })
            

9.8 函數(shù)化組件的應(yīng)用

使用context的轉(zhuǎn)變

// this.text----context.props.text
//this.$slots.default-----context.children

functional: true,表示該組件無(wú)狀態(tài)無(wú)實(shí)例

Demo

        <div id="app">
            <my-component value="hhh">
            </my-component>
        </div>

        <script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
        <script>
            Vue.component('my-component', {
                functional: true, //當(dāng)前vue實(shí)例無(wú)狀態(tài),無(wú)實(shí)例(沒(méi)有this的概念)
                render: function(creatElement, context) {
                    return creatElement('button', {
                        on: {
                            click: function() {
                                console.log(context)
                                console.log(context.parent)
                                console.log(context.props.value)
                            }
                        }
                    }, '點(diǎn)擊學(xué)習(xí)context')
                },
                props: ['value']
            })

            var app = new Vue({
                el: '#app',
                data: {
                    msg: '父組件的msg'
                }
            })
        </script>

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 前幾天想學(xué)學(xué)Vue中怎么編寫可復(fù)用的組件,提到要對(duì)Vue的render函數(shù)有所了解。可仔細(xì)一想,對(duì)于Vue的ren...
    kangaroo_v閱讀 116,533評(píng)論 13 171
  • 深入響應(yīng)式 追蹤變化: 把普通js對(duì)象傳給Vue實(shí)例的data選項(xiàng),Vue將使用Object.defineProp...
    冥冥2017閱讀 4,961評(píng)論 6 16
  • 一、了解Vue.js 1.1.1 Vue.js是什么? 簡(jiǎn)單小巧、漸進(jìn)式、功能強(qiáng)大的技術(shù)棧 1.1.2 為什么學(xué)習(xí)...
    蔡華鵬閱讀 3,500評(píng)論 0 3
  • Lua 5.1 參考手冊(cè) by Roberto Ierusalimschy, Luiz Henrique de F...
    蘇黎九歌閱讀 14,257評(píng)論 0 38
  • 孩子們今天就開(kāi)始考試了,考完明天就可以放假了。但是學(xué)校這個(gè)學(xué)期還沒(méi)有說(shuō)要開(kāi)家長(zhǎng)會(huì),結(jié)合之前開(kāi)過(guò)家長(zhǎng)會(huì)的經(jīng)驗(yàn)來(lái)看,并...
    藤縣069黎獻(xiàn)清閱讀 1,996評(píng)論 7 8

友情鏈接更多精彩內(nèi)容