vue学习--02天

一、数据绑定

`!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <script src="../js/vue.js"></script>
</head>
<body>
    <div id="root">
        <!-- 普通写法 -->
        单向数据绑定:数据从data流向页面<input type="text" v-bind:value="name">
        <br>
        <!-- v-model智能应用在表单类元素 -->
        双向数据绑定:数据从data流向页面且可以有页面流向data<input type="text" v-model:value="name" id="">
        <h2 v-bind:x="name">您好啊</h2>
        <!-- 缩写 -->
        单向数据绑定:数据从data流向页面<input type="text" :value="name">
        <br>
        <!-- v-model智能应用在表单类元素 -->
        双向数据绑定:数据从data流向页面且可以有页面流向data<input type="text" v-model="name" id="">
    </div>
    <script>
        new Vue({
            el:'#root',
            data:{
                name:'上古嘀咕'
            }
        })
    </script>
</body>
</html>`

在这里插入图片描述
二、el与data的两种写法

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <script src="../js/vue.js"></script>
</head>
<body>
    <div class="root">
        <h1>您好,{{name}}</h1>
    </div>
    <script>
        // el的两种写法
        // const v=new Vue({
        //     // el:'.root',
        //     data:{
        //         name:'张三'
        //     }
        // })
        // console.log(v)
        // v.$mount('.root')


        // data的两种写法
        new Vue({
            el:".root",
            // data的第一种写法:对象式
            // data:{
            //     name:'张三'
            // }
            // data的第二种写法:函数式(组件时必须要用函数式)
            data:function(){
                console.log(this)//这里发this是vue实例,由vue管理的函数一定不要写箭头
                return{
                    name:"张三"
                }
            }

        })
    </script>
</body>
</html>

三、事件的基本处理
1.基础知识

<script>
        new Vue({
            el:'.root',
            data:{
                name:'西安'
            },
            methods:{
                showInfo(event){
                    alert('同学您好')
                    console.log(event);
                    
                },
                showInfo2(event,number){
                    console.log(number);
                    
                    console.log(event);
                    alert('您好')
                    
                }
            }
        })
      </script>

2.键盘事件

<body>
    <div class="root">
        <h1>欢迎来到{{name}}</h1>
        <!-- 有多个单词组成的key应该用小写且中间用连字符号 -->
        <input type="text" placeholder="按下回车提示键" @keyup.caps-lock="showInfo">
        <!-- 特殊:tab键必须与keydown合作 -->
        <input type="text" placeholder="按下回车提示键" @keydown.tab="showInfo">
        <!-- 系统修饰键:ctrl,alt,shift,meta
        (1)配合keyup使用:按下修饰键的同时,再按下其他键,随后释放其他键,事件才能被触发,
        (2)配合keydown使用:正常触发事件 
        -->
        <input type="text" placeholder="按下回车提示键" @keydown.ctrl.y="showInfo">
        <input type="text" placeholder="按下回车提示键" @keyup.hua="showInfo">


    </div>
    <script>
        Vue.config.keyCodes.hua=13//定义了一个别名按键
        new Vue({
            el:'.root',
            data:{
                name:'西安'
            },
            methods:{
                showInfo(e){
                    // console.log(e.keyCode);
                    // if(e.keyCode!==13) return
                    console.log(e.target.value);
                    
                }
            }
        })
    </script>
</body>

3.事件修饰符

<body>
    <div class="root">
        <h1>欢迎来到{{name}}</h1>
        <!-- 阻止默认事件(常用) -->
        <a href="http://www.baidu.com" @click.prevent="showInfo">点我提示信息</a>
        <!-- 阻止事件冒泡(常用) -->
         <div class="demo1" @click="showInfo">
            <!-- <button @click.stop="showInfo">点我提示信息</button> -->
        <a href="http://www.baidu.com" @click.stop.prevent="showInfo">点我提示信息</a>

         </div>
         <!-- 事件只触发一次 -->
          <button @click.once="showInfo">点我提示信息</button>

          <!-- 使用事件的捕获模式 -->
        <div class="box1"@click.capture='showMessage(1)'>
         div1
         <div class="box2" @click='showMessage(2)'>
             div2
         </div>
        </div>
        <!-- 只有event.target是当前操作的元素时才出发事件 -->
        <div class="demo1" @click.self="showInfo">
            <button @click.stop="showInfo">点我提示信息</button>
         </div>

         <!-- 事件的默认行为立即执行,无需等待事件回调执行完毕 -->
          <ul class="list" @wheel.passive="demo">
            <li>1</li>
            <li>2</li>
            <li>3</li>
            <li>4</li>
          </ul>
    </div>
    <script>
        new Vue({
            el:'.root',
            data:{
                name:'西安',
            },
            methods:{
                showInfo(e){
                    // e.preventDefault()
                    alert('同学您好')
                    
                },
                showMessage(msg){
                    console.log(msg);
                    
                },
                demo(){
                    for(let i=0;i<10000000;i++){
                        console.log('#');
                        
                    }
                    console.log("@");
                    
                }
            }
        })
    </script>
</body>
  • 13
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值