欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Vue.js之render函數(shù)使用詳解

 更新時(shí)間:2021年09月14日 09:12:05   作者:貓老板的豆  
這篇文章主要介紹了Vue.js之render函數(shù)使用詳解,本篇文章通過(guò)簡(jiǎn)要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下

Vue 推薦在絕大多數(shù)情況下使用 template 來(lái)創(chuàng)建你的 HTML。然而在一些場(chǎng)景中,你真的需要 JavaScript的完全編程的能力,這就是 render 函數(shù),它比 template 更接近編譯器。

在 HTML 層, 我們決定這樣定義組件接口:通過(guò)傳入不同的level 1-6 生成h1-h6標(biāo)簽,和使用slot生成內(nèi)容

<div id="div1">
    <child :level="1">Hello world!</child>
</div>

<script type="text/x-template" id="child-template">
  <h1 v-if="level === 1">
    <slot></slot>
  </h1>
  <h2 v-if="level === 2">
    <slot></slot>
  </h2>
  <h3 v-if="level === 3">
    <slot></slot>
  </h3>
  <h4 v-if="level === 4">
    <slot></slot>
  </h4>
  <h5 v-if="level === 5">
    <slot></slot>
  </h5>
  <h6 v-if="level === 6">
    <slot></slot>
  </h6>
</script>

<script type="text/javascript">
    /**
     * 全局注冊(cè)child組件,注意template如果值以 # 開(kāi)始,則它用作選項(xiàng)符,將使用匹配元素的 innerHTML 作為模板。常用的技巧是用 <script type="x-template"> 包含模板,這樣的好處是html不會(huì)渲染里面的內(nèi)容
     * 
     * 這里使用template不是最好的選擇:
     *    一、代碼冗長(zhǎng) 
     *    二、在不同的標(biāo)題插入內(nèi)容需要重復(fù)使用slot 
     *    三、由于組件必須有根元素,所以標(biāo)題和內(nèi)容被包裹在一個(gè)無(wú)用的div中,比如<div><h1>hello world</h1></div>
     */

    Vue.component('child', {
      template: '#child-template',
      props: {
        level: {
          type: Number,
          required: true
        }
      },
      data: function() {
        return {
          a: 1
        }
      }
    })

    new Vue({
        el:"#div1"
    })
  </script>

我們嘗試使用render函數(shù)實(shí)現(xiàn)上面的例子,注意使用render函數(shù),template 選項(xiàng)將被忽略。

createElement接收3個(gè)參數(shù):

第一個(gè)參數(shù)可以是HTML標(biāo)簽名,組件或者函數(shù)都可以;此參數(shù)是必須的;
第二個(gè)為數(shù)據(jù)對(duì)象{Object}(可選);

示例如下:

<div id="div1">
    <child :level="1">
        Hello world!
    </child>
    <child :level="2">
        <!-- 將不會(huì)被顯示 -->
        <span slot="footer">this is footer slot</span>
        <p slot="header">this is header slot</p>
    </child>
</div>


<script>
    Vue.component('child', {
        render: function (createElement) {
            console.log(this.$slots);
            return createElement(
                'h' + this.level, // tagName標(biāo)簽名稱(chēng)
                {
                    // 為每個(gè)h標(biāo)簽設(shè)置class
                    'class': {
                        foo: true,
                        bar: false
                    },
                    // 最終被渲染為內(nèi)聯(lián)樣式
                    style: {
                        color: 'red',
                        fontSize: '14px'
                    },
                    // 其他的html屬性
                    attrs: {
                        id: 'foo',
                        'data-id': 'bar'
                    },
                    // DOM屬性
                    domProps: {
                        // innerHTML: 'from domProps',
                    },
                    // 事件監(jiān)聽(tīng)器基于 "on"
                    // 所以不再支持如 v-on:keyup.enter 修飾器
                    on: {
                        click: this.clickHandler
                    },
                    // ...
                },
                // 你可以從this.$slots獲取VNodes列表中的靜態(tài)內(nèi)容
                // $slots.default用來(lái)訪(fǎng)問(wèn)組件的不具名slot
                // 當(dāng)你可能需要具名slot的時(shí)候需要指定slot的name, this.$slots.header
                // [this.$slots.default,this.$slots.footer,this.$slots.header] //顯示level2插槽
                [this.$slots.default]  //只顯示不具名slot
            )
        },
        template: '<div v-if="level===1"><slot></slot></div>', // 將被忽略
        props: {
            level: {
                type: Number,
                required: true
            }
        },
        methods: {
            clickHandler: function () {
                console.log('clickHandler')
            }
        }
    })

    new Vue({
        el: "#div1"
    })
</script>

渲染如下:

這里寫(xiě)圖片描述

到此這篇關(guān)于Vue.js之render函數(shù)使用詳解的文章就介紹到這了,更多相關(guān)Vue.js之render函數(shù)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論