玩轉(zhuǎn)Vue_render函數(shù)

新創(chuàng)建一個(gè)html,利用之前學(xué)過的組件注冊完成渲染

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
</head>
<body>
    <div id='app'>
        <login></login>
    <div>
    
    <script src='./js/vue.js'></script>
    <script>

        const login = {
            template : '<h1>這是一個(gè)登錄組件</h1>'
        }

        const vm = new Vue({
            el : '#app', // vue實(shí)例控制的區(qū)域,稱為 掛載元素
            data : {},
            methods: {},
            components : {
                login
            }
        })
    </script>
</body>
</html>

使用render函數(shù)也可以渲染組件

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
</head>
<body>
    <div id='app'>
        <p>3333</p>
    <div>
    
    <script src='./js/vue.js'></script>
    <script>

        const login = {
            template : '<h1>這是一個(gè)登錄組件</h1>'
        }

        const vm = new Vue({
            el : '#app', // vue實(shí)例控制的區(qū)域,稱為 掛載元素
            data : {},
            methods: {},
            render(createElement) {
                return createElement(login)
            },
        })
    </script>
</body>
</html>

區(qū)別 : 使用render渲染的組件會完全替換el掛載元素中的內(nèi)容,而第一種方法不會(看不見p標(biāo)簽中的333,因?yàn)楸惶鎿Q了)

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

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

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