1. 样式绑定

1.1 class绑定

使用方式:v-bind:class="expression"

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

1.2 style绑定

v-bind:style="expression"

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

2. 事件处理器

事件监听可以使用v-on 指令

## 之前已学习

2.1 事件修饰符

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>

2.2 按键修饰符

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">
        <title>样式绑定和事件处理</title>
        <script src="https://cdn.bootcss.com/vue/2.6.10/vue.js"></script>
        <style type="text/css">
            .a {
                color: red;
            }
            
            .b {
                color: blue;
            }
            
            .c {
                font-size: 60px;
            }
            
            
        </style>
    </head>

    <body>
        <div id="app">
            <ul>
                <li>
                    <h3>样式绑定</h3>
                    <span :class="aClz">犯我华夏</span>
                    <span :class="bClz">虽远必诛</span>
                    <span :class="cClz">犯我华夏,虽远必诛</span>
                </li>
                <li>
                    <h3>事件处理-阻止冒泡</h3>
                    <div style="background-color: #0000FF;width: 600px;height: 600px;" @click="mya">
                        <div style="background-color: #FF0000;width: 500px;height: 500px; padding: 50px;"  @click="myb">
                            <div style="background-color:blue;width: 400px;height: 400px;padding: 50px;" @click="myc">
                                <div style="background-color:yellow;width: 300px;height: 300px;padding: 50px;" @click.stop="myd">

                                </div>
                            </div>
                        </div>
                    </div>
                </li>
                <li>
                    <h3>事件处理-按钮只能处理一次</h3> 
                        {{info}} <input type="text" v-model="msg" />
                        <button @click="mye">发送</button>
                        <button @click.once="mye">发送(只能点一次)</button>
                    </li>
                <li>
                    <h3>按键修饰符</h3>
                    {{info}} <input type="text" v-model="msg" v-on:keyup.enter="mye" />
                        <button @click="mye">发送</button>
                        <button @click.once="mye">发送(只能点一次)</button>
                </li>
                <li>
                    <h3>select标签</h3>
                    <select name="hobby" v-model="selectIds">
                        <option v-for="d in datas" :value="d.id">{{d.name}}</option>
                    </select>
                    选中的值{{selectIds}}
                </li>
                <li>
                    <h3>复选框标签</h3>
                    <div v-for="d in datas">
                        <input type="checkbox" name="likes"  :value="d.id" v-model="selectArr"/>{{d.name}}
                    </div>
                    选中的值:{{selectArr}}
                </li>
            </ul>
            
        </div>
    </body>
    <script type="text/javascript">
        new Vue({
            el: '#app',
            data: {
                aClz: 'a',
                bClz: 'b',
                cClz: ['b', 'c'],
                msg: '',
                info: '',
                datas:[{
                    id:1,
                    name:'象棋'
                },{
                    id:2,
                    name:'军旗'
                },{
                    id:3,
                    name:'跳棋'
                }],
                selectIds:'',
                selectArr:[]
            },
            methods: {
                mya() {
                    console.log('a事件触发')
                    alert(this.selectIds)
                },
                myc() {
                    console.log('b事件触发')
                },
                myb() {
                    console.log('c事件触发')
                },
                myd() {
                    console.log('d事件触发')
                },
                mye() {
                    this.info = this.msg;
                    this.msg = ''
                }
            },

        })
    </script>

</html>

 

3. vue表单

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

3.1 常用控件

文本框/密码框/文本域/隐藏域

单选复选框/多选复选框

单选按钮

下拉框

3.2 修饰符

.lazy

默认情况下, v-model在input事件中同步输入框的值与数据,但你可以添加一个修饰符lazy,从而转变为在change事件中同步

.number

将用户的输入值转为 Number 类型

.trim

自动过滤用户输入的首尾空格

 

注1:H5提供的新方法

JSON.parse():只能解析json形式的字符串变成json,安全性高一些

JSON.stringify():json转换为字符串,解析的时候会自动加上引号

 代码演示:

<!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>

4. vue组件

4.1 组件简介

组件(Component)是Vue最强大的功能之一

组件可以扩展HTML元素,封装可重用的代码

组件系统让我们可以用独立可复用的小组件来构建大型应用,几乎任意类型的应用的界面都可以抽象为一个组件树

4.2 全局和局部组件

全局组件:Vue.component(tagName, options),tagName为组件名,options为配置选项。

局部组件: new Vue({el:'#d1',components:{...}})

 

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

<tagName></tagName>

4.3 props

props是父组件用来传递数据的一个自定义属性。

父组件的数据需要通过props把数据传给子组件,子组件需要显式地用props选项声明 "prop"

 

注1:因为组件是可复用的 Vue 实例,所以它们与 new Vue 接收相同的选项,例如 data、computed、watch、methods

以及生命周期钩子等。仅有的例外是像el这样根实例特有的选项。

注2:当我们定义这个 <button-counter> 组件时,你可能会发现它的data并不是像这样直接提供一个对象

data: {

count: 0

}

取而代之的是,一个组件的data选项必须是一个函数,因此每个实例可以维护一份被返回对象的独立的拷贝:

data: function () {

return {

count: 0

}

}

 

注3:定义组件名的方式有两种

短横线分隔命名(建议使用)

Vue.component('my-component-name', { /* ... */ }),引用方式:<my-component-name>

首字母大写命名

Vue.component('MyComponentName', { /* ... */ }),引用方式: <my-component-name>和<MyComponentName>都是可接受的

注4:HTML 中的特性名是大小写不敏感的,所以浏览器会把所有大写字符解释为小写字符。这意味着当你使用 DOM 中的模板时,

camelCase (驼峰命名法) 的 prop 名需要使用其等价的 kebab-case (短横线分隔命名) 命名:

props: ['postTitle'],<my-tag post-title="hello!"></my-tag>

注5:props: ['title', 'likes', 'isPublished', 'commentIds', 'author']

希望每个 prop 都有指定的值类型

props: {

title: String,

likes: Number,

isPublished: Boolean,

commentIds: Array,

author: Object,

callback: Function,

contactsPromise: Promise // or any other constructor

}

代码演示:

<!DOCTYPE html>
<html>

    <head>
        <meta charset="UTF-8">
        <title>组件</title>
        <script src="https://cdn.bootcss.com/vue/2.6.10/vue.js"></script>

    </head>

    <body>
        <div id="app">
            <ul>
                <li>
                    <h3>局部组件的注册</h3>
                    <!--侵入性-->
                    <my-button></my-button>
                    
                </li>
                <li>
                    <h3>全局组件的注册</h3>
                    <my-button2></my-button2>

                </li>
                <li>
                    <h3>组件的通信(父传子)</h3>
                    <my-button m='zs'></my-button>
                </li>
                <li>
                    <h3>组件的通信(子传父)</h3>
                    <my-button m='zs' @three-click='xxx'></my-button>
                </li>
        </div>
    </body>
    <script type="text/javascript">
        Vue.component('my-button2',{
            template: '<button v-on:click="doSubmit">被点了{{n}}次</button>',
                    data(){
                        return {
                            n:0
                        }
                    },
                    methods:{
                        doSubmit(){
                            this.n+=1;
                        }
                    }
        })
        new Vue({
            el: '#app',
            data(){
                return{
                    
                }
            },
            components: {
                'my-button': {
                    props:['m'],
                    template: '<button v-on:click="doSubmit">被{{m}}点了{{n}}次</button>',
                    data(){
                        return {
                            n:0,
                            zedie:'折叠效果'
                        }
                    },
                    methods:{
                        doSubmit(){
                            this.n+=1;
                            //注册一个事件,让外部调用,然后顺便接受内部的值
                            if(this.n % 3==0){
                                this.$emit('three-click',this.zedie);
                            }
                        }
                    }
                }
            },
            methods:{
                xxx(v){
                    alert(v)
                }
            }

        })
    </script>

</html>

 

posted on 2019-08-23 09:21  旧城微冷  阅读(173)  评论(0编辑  收藏  举报