10.9 Vue预热

1 Array的方法

主要掌握 forEach() map()  filter() every() some()    参数都是function(item,index,arr){}

reduce() concat()

2 defineProperty

Object.defineproperty    

 具体文档  https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Object/defineProperty

3 数据劫持简单版(双向数据绑定简单版)

<!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>
    <input type="text" id="inp" name="">
    <div id="main">
        {{ name }}
        {{ name }}
        {{ name }}
    </div>

    <script>
        //1 初始化数据
        var data ={
            name:'penkey'
        }
        //Object.defineProperty
        //2 数据劫持
        var val = null;
        var str = main.innerHTML;
        // console.log(str)
        Object.defineProperty(data,"name",{ 
            get:function(){
                return val;
            },
            set:function(newVal){
                val = newVal;
                complie();
            },

        })
        //初始化劫持数据
        data.name = 'penkey';
        //初始化文本框内容
        inp.value = data.name;

        //模板编译
        function complie(){
            main.innerHTML = str.replace(/\{\{\s*([^\s]+)\s*\}\}/g,function(){
                return data[arguments[1]];
            })
        }

        //修改视图文本框
        inp.onkeyup = function(){
            data.name = this.value;
        }


    </script>

</body>
</html>

Vue尝鲜

1 声明式渲染    模板中先声明变量然后再把数值复制过去

2 响应式  数据视图同时变

指令:Vue中v-开头的都叫指令

创建Vue实例  虽然没有完全遵循 MVVM 模型,但是 Vue 的设计也受到了它的启发。因此在文档中经常会使用 vm (ViewModel 的缩写) 这个变量名表示 Vue 实例。

 $el: 是指Vue活动的范围

$options 是创建vue实例传入的参数

 

 

$el :

function Vue(options){
            this.$options = options;
            //this.$el = options.el;
            this.$data = options.data;


            function getEl(el)
            {
                if(typeof el == 'string')
                {
                    if(el !="body" && el!="html")
                    {
                        return document.querySelector(el)
                    }
                    else{
                        throw new Error("error");
                        
                    }
                }
                else
                {
                    return el;
                }
            }
            this.$el=getEl(options.el)
        }

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值