Vue-createElement源码解析

vue中 render函数实际上return 一个createElement函数  

对createElement函数进行简单解析

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<style>
    .textarea{
        width: 400px;
        height: 300px;
        resize: none;
    }
</style>
<body>
    <div id="app"></div>
    <script>
        var vDom = createElement('div',{
            class:"container"
        },[
            createElement("h2",{
                class:"item",
                style:"background:yellow"
            },'四大名著'),
            createElement("ul",{
                class:"item"
            },[
                createElement("li",{
                    class:"item"
                },'三国演义'),
                createElement("li",{
                    class:"item"
                },'水浒传'),
                createElement("li",{
                    class:"item"
                },'红楼梦'),
                createElement("li",{
                    class:"item"
                },['西游记'])
            ]),
            createElement('textarea',{
                class:'textarea',
                style:'line-height:20px;text-indent:15px',
                placeholder:'你最喜欢哪一部,并写出读后感'
            })
        ])

        function Element(type,props,children){
            this.type=type
            this.props = props
            this.children = children
        }
        function createElement(type,props,children) {
            return new Element(type,props,children)
        }
        function render(obj) {
            // 创建节点
            let el = document.createElement(obj.type)
            // 给相关的节点设置对应的属性
            for(let key in obj.props){
                console.log(key,'56')
                //console.log(el)
                el.setAttribute(key,obj.props[key])
            }
            if(Array.isArray(obj.children)){
                // 遍历创建子元素
                obj.children.forEach((child) => {
                    // 递归操作,如果当前的child不是文本,就继续进行递归操作,否则,渲染文本节点
                    child = child instanceof Element ? render(child) : document.createTextNode(child)
                     // 节点上树
                    el.appendChild(child)
                })
            }
            // 当前obj.children如果不是数组,是字符串,就当做文本进行渲染
            else if(typeof obj.children === 'string'){
                el.appendChild(document.createTextNode(obj.children))
            }
            return el
        }
        // 渲染节点
        function renderDom(el,target) {
            target.appendChild(el)
        }
        console.log(renderDom(render(vDom),document.getElementById("app")))
    </script>
</body>
</html>

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值