Vue的基本语法(二),双向绑定

文章目录

      • 事件修饰符:
  • Vue指令之`v-model`和`双向数据绑定`
  • 例子:简易计算器

事件修饰符:

  • .stop 阻止冒泡
  • .prevent 阻止默认事件
  • .capture 添加事件侦听器时使用事件捕获模式
  • .self 只当事件在该元素本身(比如不是子元素)触发时触发回调
  • .once 事件只触发一次

<html lang="zh-CN">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0,minimal-ui:ios">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Documenttitle>
    <script src="./lib/vue-2.4.0.js">script>
    <style>
        .inner{
            height: 150px;
            background-color: darkcyan;
        }
        .outer{
            padding: 50px;
            background-color: red;
        }
    style>
head>

<body>

    <div class="fd-app">
        <div class="inner" @click="div1Handler">
            
            <button @click.stop="btnHandler">戳他button>
        div>
        
        <a href="http://www.baidu.com" @click.prevent="linkClick">有问题,找百度a>
        
        <div class="inner" @click.capture="div1Handler">
            <button @click="btnHandler">戳他button>
        div>
        
        <div class="inner" @click.self="div1Handler">
            <button @click="btnHandler">戳他button>
        div>
        
        <a href="http://www.baidu.com" @click.prevent.once="linkClick">有问题,找百度a>

        
        
        <div class="outer" @click="div2Handler">
            <div class="inner" @click.self="div1Handler">
                <button @click="btnHandler">戳他button>
            div>
        div>
    div>

    <script>
        var vm = new Vue({
            el:".fd-app",
            data:{},
            methods:{
                div1Handler(){
                    console.log('这是触发了inner div的点击事件');
                },
                btnHandler(){
                    console.log('这是触发了按钮的点击事件');
                },
                linkClick(){
                    console.log('出发了链接的点击事件');
                },
                div2Handler(){
                    console.log('这是触发了outer div的点击事件');
                }
            }
        })
    script>

body>

html>

Vue指令之v-model双向数据绑定


<html lang="zh-CN">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0,minimal-ui:ios">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Documenttitle>
    <script src="./lib/vue-2.4.0.js">script>
head>

<body>

    <div class="fd-app">
        <h4>{{msg}}h4>
        
        
        
        <input type="text" v-model="msg" style="width: 100%">
        

        <select v-model="selected">
            <option v-for="option in options" v-bind:value="option.value">
                {{ option.text }}
            option>
        select>
        <span>Selected: {{ selected }}span>
    div>

    <script>
        var vm = new Vue({
            el:".fd-app",
            data:{
                msg:'大家都是好学生,爱敲代码,爱学习。',
                selected: 'A',
                options: [
                    { text: 'One', value: 'A' },
                    { text: 'Two', value: 'B' },
                    { text: 'Three', value: 'C' }
                ]
            },
            methods:{
            }
        })
    script>

body>

html>

例子:简易计算器


<html lang="zh-CN">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0,minimal-ui:ios">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Documenttitle>
    <script src="./lib/vue-2.4.0.js">script>
head>

<body>

    <div class="fd-app">
        <input type="text" v-model="n1">
        <select v-model="opt">
            <option value="+">+option>
            <option value="-">-option>
            <option value="*">*option>
            <option value="/">/option>
        select>

        <input type="text" v-model="n2">
        <input type="button" value="=" @click="calc">
        <input type="text"  v-model="result">

    div>

    <script>
        var vm = new Vue({
            el:".fd-app",
            data:{
                n1:0,
                n2:0,
                result:0,
                opt: '+'
            },
            methods:{

                /*calc(){ //计算器算数的方法
                    //逻辑:
                    switch(this.opt){
                        case '+':
                            this.result = parseInt(this.n1) + parseInt(this.n2);
                            break;
                        case '-':
                            this.result = parseInt(this.n1) - parseInt(this.n2); 
                            break;
                        case '*':
                            this.result = parseInt(this.n1) * parseInt(this.n2);
                            break;
                        case '/':
                            this.result = parseInt(this.n1) / parseInt(this.n2);
                            break;
                    }
                }*/
                //注意:这是投机取巧的方式,正式开发中尽量少用。
                calc(){
                    var codeStr = 'parseInt(this.n1)' + this.opt + 'parseInt(this.n2)';
                    this.result = eval(codeStr);
                }
            }
        })
    script>

body>

html>

你可能感兴趣的:(Vue)