• Vue基础语法2


    Vue基础语法1:

    https://www.cnblogs.com/psyu/p/11283220.html

    一、样式绑定

    class绑定

    使用方式:v-bind:class=“expression”

    expression的类型:字符串、数组、对象

    style绑定

    v-bind:style=“expression”

    expression的类型:字符串、数组、对象

    <!DOCTYPE html>
    <html>
        <head>
            <meta charset="utf-8">
            <script src="https://cdn.bootcss.com/vue/2.6.10/vue.js"></script>
            <title>样式绑定</title>
            <style>
                .a{
                    color: red;
                }
                .b{
                    color: green;
                }
                .c{
                    font-size: 50px;
                }
                
            </style>
        </head>
        <body>
            <div id="app">
                <ul>
                    <li>
                        <h3>文本</h3>
                           {{msg}}
                    </li>
                    <li>
                        <h3>样式一</h3>
                          <div :class="xa">{{msg}}</div> 
                    </li>
                    <li>
                        <h3>样式二</h3>
                          <div :class="xb">{{msg}}</div> 
                    </li>
                    <li>
                        <h3>样式三</h3>
                          <div :class="xc">{{msg}}</div> 
                    </li>
                    
                </ul>
            </div>
        </body>
        <script type="text/javascript">
            new Vue({
                el: "#app",
                data() {
                    return {
                        msg: 'hello vue',
                        xa:'a',
                        xb:'b',
                        xc:[ 'a','c']
                    };
                }
            });
        </script>
    </html>

    二、事件处理器

    之前已学习
        事件修饰符
          Vue通过由点(.)表示的指令后缀来调用修饰符,
          .stop
          .prevent
          .capture
          .self
          .once 
     
          <!-- 阻止单击事件冒泡 -->
          <a v-on:click.stop="doThis"></a>
          <!-- 提交事件不再重载页面 -->
          <form v-on:submit.prevent="onSubmit"></form>
          <!-- 修饰符可以串联  -->
          <a v-on:click.stop.prevent="doThat"></a>
          <!-- 只有修饰符 -->
          <form v-on:submit.prevent></form>
          <!-- 添加事件侦听器时使用事件捕获模式 -->
          <div v-on:click.capture="doThis">...</div>
          <!-- 只当事件在该元素本身(而不是子元素)触发时触发回调 -->
          <div v-on:click.self="doThat">...</div>
          <!-- click 事件只能点击一次 -->
          <a v-on:click.once="doThis"></a>
    
            
        按键修饰符
          Vue允许为v-on在监听键盘事件时添加按键修饰符:
    
          <!-- 只有在 keyCode 是 13 时调用 vm.submit() -->
          <input v-on:keyup.13="submit">
    
          Vue为最常用的按键提供了别名
          <!-- 同上 -->
          <input v-on:keyup.enter="submit">
    
          全部的按键别名:
          .enter
          .tab
          .delete (捕获 "删除""退格" 键)
          .esc
          .space
          .up
          .down
          .left
          .right
          .ctrl
          .alt
          .shift
          .meta      
    <!DOCTYPE html>
    <html>
        <head>
            <meta charset="utf-8">
            <script src="https://cdn.bootcss.com/vue/2.6.10/vue.js"></script>
            <title>样式绑定</title>
            <style>
                div{
                    padding: 40px;
                }
                
            </style>
        </head>
        <body>
            <div id="app">
                <ul>
                    <li>
                        <h3>文本</h3>
                        {{msg}}
                    </li>
                    <li>
                        <h3>防止事件冒泡</h3>
                        <div style="height: 300px;  300px; background-color: orange;" @click="a">
                            <div style="height: 200px;  200px; background-color: green;" @click="b">
                                <div style="height: 100px;  100px; background-color: red;" @click="c">
                                    <div style="height: 30px;  30px; background-color: pink;" @click.stop="d">
    
                                    </div>
                                </div>
                            </div>
                        </div>
                    </li>
                    <li>
                        <h3>事件只能点击一次(模拟聊天信息发送)</h3>
                        {{qqmsg}}
                        <input type="text" v-on:keyup.enter="send" v-model="msg" />
                        <button @click="send">ok</button>
                        <button @click.once="send">点一次</button>
                    </li>
                    <li>
                        <h3>表单的复选框</h3>
                        <div v-for="item,index in hobby">
                            <input v-model="checkedIds" type="checkbox" name="hobby" :value="item.id" />
                            {{item.name}}
                        </div>
                        {{checkedIds}}
                    </li>
                    <li>
                        <h3>表单中的下拉框讲解</h3>
                        <select name="likes" v-model="selectedId">
                            <option v-for="item,index in hobby" :value="item.id">{{item.name}}</option>
                        </select>
                        {{selectedId}}
                    </li>
                </ul>
            </div>
        </body>
        <script type="text/javascript">
            new Vue({
                el: "#app",
                data() {
                    return {
                        msg: 'hello vue',
                        qqmsg: null,
                        hobby: [{
                            id: '1 ',
                            name: '篮球'
                        }, {
                            id: '2',
                            name: ''
                        }, {
                            id: '3',
                            name: 'rap'
                        }],
                        checkedIds:[],
                        selectedId:null
                    };
                },
                methods: {
                    a() {
                        alert('a')
                    },
                    b() {
                        alert('b')
                    },
                    c() {
                        alert('c')
                    },
                    d() {
                        alert('d')
                    },
                    send() {
                        this.qqmsg = this.msg;
                        this.msg = null
                    },
                },
            })
        </script>
    </html>

     

    三、Vue表单

    用v-model指令在表单控件元素上创建双向数据绑定

    <!DOCTYPE html>
    <html>
        <head>
            <meta charset="utf-8">
            <script src="https://cdn.bootcss.com/vue/2.6.10/vue.js"></script>
            <title>表单</title>
        </head>
        <body>
            <div id="app">
                <h1>标题</h1>
                <ul>
                    <li>
                        <p>vue表单</p>
                        <label>姓名:</label><input v-model="uname" /><br />
                        <label>密码:</label><input v-model="upwd" type="password" /><br />
                        <!-- 将用户的输入值转为 Number 类型 -->
                        <label>年龄:</label><input v-model.number="age" /><br />
                        <label>性别:</label>
                        <input type="radio" v-model="sex" name="sex" value="1" /><input type="radio" v-model="sex" name="sex" value="0" />女<br />
                        <label>爱好:</label>
                        <div v-for="h in hobby">
                            <input type="checkbox" v-model="hobbies" v-bind:value="h.id" />{{h.name}}
                        </div>
                        <label>类别:</label>
                        <select v-model="type">
                            <option value="-1">===请选择===</option>
                            <option v-for="t in types" v-bind:value="t.id">{{t.name}}</option>
                        </select><br />
                        <label>备注:</label>
                        <textarea v-bind:value="mark"></textarea><br />
                        确认<input type="checkbox" v-model="flag" />
                        <input type="submit" v-bind:disabled="show" v-on:click="doSubmit" />
                    </li>
                </ul>
            </div>
        </body>
        <script type="text/javascript">
            new Vue({
                el: '#app',
                data() {
                    return {
                        uname: null,
                        upwd: null,
                        age: 10,
                        sex: 1,
                        hobby: [{
                            id: 1,
                            name: '篮球'
                        }, {
                            id: 2,
                            name: '足球'
                        }, {
                            id: 3,
                            name: '象棋'
                        }],
                        hobbies: [],
                        types: [{
                            id: 1,
                            name: 'A'
                        }, {
                            id: 2,
                            name: 'B'
                        }, {
                            id: 3,
                            name: 'C'
                        }],
                        type: null,
                        mark: '学生备注',
                        flag: false
                    }
                },
                computed: {
                    show: function() {
                        return !this.flag;
                    }
                },
                methods: {
                    doSubmit: function() {
                        console.log('doSubmit')
                        var obj = {
                            uname: this.uname,
                            upwd: this.upwd,
                            age:this.age+10,
                            sex: this.sex,
                            hobbies:this.hobbies,
                            type: this.type,
                            mark: this.mark,
                        }
                        console.log(obj);
                    }
                }
    
            })
        </script>
    </html>

    四、Vue组件

     组件简介
    组件(Component)是Vue最强大的功能之一
    组件可以扩展HTML元素,封装可重用的代码
    组件系统让我们可以用独立可复用的小组件来构建大型应用,几乎任意类型的应用的界面都可以抽象为一个组件树

     全局和局部组件
    全局组件:Vue.component(tagName, options),tagName为组件名,options为配置选项。
    局部组件: new Vue({el:’#d1’,components:{…}})

    注册后,我们可以使用以下方式来调用组件: 
      <tagName></tagName>

    props
    props是父组件用来传递数据的一个自定义属性。
    父组件的数据需要通过props把数据传给子组件,子组件需要显式地用props选项声明 “prop”

    <!DOCTYPE html>
    <html>
        <head>
            <meta charset="utf-8">
            <script src="https://cdn.bootcss.com/vue/2.6.10/vue.js"></script>
            <title>组件</title>
        </head>
        <body>
            <div id="app">
                <h1>组件</h1>
                <ul>
                    <li>
                        <p>局部vue组件</p>
                        <my-button></my-button>
                    </li>
                    <li>
                        <p>父组件传值子组件</p>
                        <my-button m="旺旺"></my-button>
                    </li>
                    <li>
                        <p>子组件传值父组件</p>
                        <my-button @three-click="doSSS"></my-button>
                    </li>
                    <li>
                        <p>全局vue组件的注册</p>
                        <my-button2></my-button2>
                    </li>
                </ul>
            </div>
        </body>
        <script type="text/javascript">
            Vue.component('my-button2',{
                        props:['m'],
                        template:'<button @click="doXXX">自定义按钮,被{{m}}点击了{{n}}次</button>',
                        data() {
                            return {
                                n:0
                            };
                        },
                        methods:{
                            doXXX(){
                                this.n++;
                                this.$emit('three-click',this.n,'dudu','描述');
                            },
                        }
                    })
            new Vue({
                el: '#app',
                data() {
                    return {
                        msg:'hello vue 组件',
                    }
                },
                computed:{
                    xs(){
                        return this.n;
                    }
                },
                methods:{
                    doSSS(n,x,y){
                        alert(n);
                        alert(x);
                        alert(y);
                    }
                },
                components: {
                    'my-button':{
                        props:['m'],
                        template:'<button @click="doXXX">自定义按钮,被{{m}}点击了{{n}}次</button>',
                        data() {
                            return {
                                n:0
                            };
                        },
                        methods:{
                            doClick(){
                                this.n++;
                                this.$emit('three-click',this.n,'dudu','描述');
                            },
                        }
                    }
                    
                }
    
            })
        </script>
    </html>
  • 相关阅读:
    查询中常用的扩展方法
    加载关联表的数据 显式加载
    加载关联表的数据 延迟加载
    加载关联表的数据 贪婪加载
    操作内存中的数据
    DBContext基础查询
    EF简单增删改查
    1- MySQL数据库基础快速入门
    1-3 Postman 注册账号与登录
    1-2 postman工具简介
  • 原文地址:https://www.cnblogs.com/psyu/p/11293856.html
Copyright © 2020-2023  润新知