【Vue温故】粗略实现Vue源码v-if-v-show及生命周期

<!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>
<body>
    <div id="app"></div>
</body>
<script>

    var Vue =(function(){
      function Vue(options){
        var recycles={
        //   beforeCreate:options.beforeCreate.bind(this),
        //   created:options.created.bind(this),
        //   beforeMount:options.beforeMount.bind(this),
        //   mounted:options.mounted.bind(this)
        }
        //recycles.beforeCreate();
        this.$el = document.querySelectorAll(options.el)[0];
        this.$data = options.data();
        this._init(this,options.template,options.methods,recycles)
      }
      Vue.prototype._init = function(vm,template,methods,recycles){
      //  recycles.created();
        var container = document.createElement('div');
        container.innerHTML = template;
    
        var showPool = new Map();
        var eventPool = new Map();
    
        initData(vm,showPool);
        initPool(container,methods,showPool,eventPool);
        bindEvent(vm,eventPool);
        render(vm,showPool,container,recycles)
      }
      function initData(vm,showPool){
        var _data = vm.$data;
        for(var key in _data){
          (
            function(key){
              Object.defineProperty(vm,key,{
                get:function(){
                  return _data[key];
                },
                set:function(newValue){
                  _data[key] = newValue;
                  update(vm,key,showPool)
                }
              })
            }
          )(key)
        }
      }
      function initPool(container,methods,showPool,eventPool){
          var _allNodes = container.getElementsByTagName('*');
          var dom = null;
          for(var i = 0;i < _allNodes.length; i++){
            dom = _allNodes[i];
            var vIfData = dom.getAttribute('v-if'),
                vShowData = dom.getAttribute('v-show'),
                vEvent = dom.getAttribute('@click');
            if(vIfData){
                
              showPool.set(dom,
                {
                  type:'if',
                  prop:vIfData
                }
              )
              dom.removeAttribute('v-if');
            }else if(vShowData){
              showPool.set(dom,
                {
                  type:'show',
                  prop:vShowData
                }
              )
              dom.removeAttribute('v-show');
            }else if(vEvent){
              eventPool.set(dom,
                methods[vEvent]
              )
              dom.removeAttribute('@click');
            }
          }
      }
      function bindEvent(vm,eventPool){
        for(var [dom,handler] of eventPool){
          vm[handler.name] = handler;
          dom.addEventListener('click',vm[handler.name].bind(vm),false); 
        }
      }
      function render(vm,showPool,container,recycles){
          console.log(showPool);
        var _data = vm.$data,
            _el = vm.$el;
        for(var [dom,info] of showPool){
          switch(info.type){
            case 'if':
              info.comment = document.createComment(['v-if']);
              !_data[info.prop] && dom.parentNode.replaceChild(info.comment,dom);
              break;
            case 'show':
              !_data[info.prop] && (dom.style.display = 'none')
              break;
            default:
              break;
          }
        }
       // recycles.beforeMount();
        _el.appendChild(container);
       
       // recycles.mounted();
      }
      function update(vm,key,showPool){
        var _data = vm.$data;
        for(var [dom,info] of showPool){
          if(info.prop == key){
            switch(info.type){
              case 'if':
                !_data[key] ? dom.parentNode.replaceChild(info.comment,dom)
                            : info.comment.parentNode.replaceChild(dom,info.comment)
              break;
              case 'show':
                !_data[key] ? (dom.style.display = 'none')
                            : (dom.removeAttribute('style'))
               break;
              default:
                break;
            }
          }
        }
      }
      return Vue;
    })()
    new Vue({
        el:'#app',
        data(){
            return{
                ifShow1:false,
                ifShow2:false
            }
        },
        methods:{
            show1(){
                this.ifShow1 = true;
            },
            show2(){
                this.ifShow2 =true
            }
        },
        template:
            "<div className='xx'><div v-if='ifShow1'>1111111</div></div><div v-if='ifShow2'>222222</div><button @click='show1'>1show</button><button @click='show2'>2show</button>",
        
        
        
    })
</script>

</html>

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值