vue渲染方式:render和template的區別


    template----html的方式做渲染
    render----js的方式做渲染

    render(提供)是一種編譯方式
    render里有一個函數h,這個h的作用是將單文件組件進行虛擬DOM的創建,然后再通過render進行解析。
    h就是createElement()方法:createElement(標簽名稱,屬性配置,children)

    template也是一種編譯方式,但是template最終還是要通過render的方式再次進行編譯。
 

區別:

        1、render渲染方式可以讓我們將js發揮到極致,因為render的方式其實是通過createElement()進行虛擬DOM的創建。邏輯性比較強,適合復雜的組件封裝。
        2、template是類似於html一樣的模板來進行組件的封裝。
        3、render的性能比template的性能好很多
        4、render函數優先級大於template
 

案例一:template和render的方式渲染標題標簽:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <div id="app">
        <h-title level="1">標題</h-title>
        <h-title level="2">標題</h-title>
        <h-title level="3">標題</h-title>
    </div>
    <script src="https://unpkg.com/vue/dist/vue.js"></script>
    <script>
        Vue.component("h-title",{
            /*   template渲染   */
            // template:`
            //     <div>
            //         <h1 v-if="level==1"><slot></slot></h1>    
            //         <h2 v-else-if="level==2"><slot></slot></h2>    
            //         <h3 v-else-if="level==3"><slot></slot></h3>    
            //     </div>
            // `,
            
            /*   render渲染   */
            render:function(h){
                // createElement(標簽名稱,屬性配置,children)
                return h("h"+this.level,
                    {
                        attrs:{
                            "data-id":10
                        }
                    },
                    // 相當於<slot></slot>標簽接收
                    this.$slots.default
                )
            },
            props:{
                level:{
                    type:String
                }
            }
        });
        let vm=new Vue({
            el:"#app"
        });
    </script>
</body>
</html>

 

案例二:render方式模擬button:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        *{margin: 0;padding: 0;}
        .btn{
            width: 80px;
            line-height: 40px;
            text-align: center;
            color:#fff;
            border-radius: 5px;
            background-color: #ccc;
        }
        .success{background-color: green;}
        .error{background-color: red;}
        .info{background-color: pink;}
    </style>
</head>
<body>
    <div id="app">
        <wql-button type="success">成功</wql-button>
        <wql-button type="info">提示</wql-button>
        <wql-button type="error">報錯</wql-button>
        <wql-button>默認</wql-button>
    </div>
    <script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
    <script>
        Vue.component("wql-button",{
            render:function(h){
                return h("div",{
                    class:{
                        btn:true,
                        success:this.type=="success",
                        error:this.type=="error",
                        info:this.type=="info"
                    }
                },this.$slots.default);
            },
            props:{
                type:{
                    type:String
                }
            }
        });
        let vm=new Vue({
            el:"#app"
        });
    </script>
</body>
</html>

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM