vue初学第二天

Posted on 2020-10-13 09:02  lhc1994  阅读(31)  评论(0)    收藏  举报
 

1.表单的使用

 
 
<div id="app">
        <h2>添加学员</h2>
        <span>姓名</span> <input type="text" v-model='user.name'>
        <br>
        <!-- 对于单选框来说,选中的时候,会把自己的value值赋值给变量 -->
        <span>性别</span> <input type="radio" v-model='user.sex' value="男">男<input type="radio" v-model='user.sex' value="女">女
        <br>
        <span>电话</span> <input type="text" v-model='user.tel'>
        <br>
        <span>密码</span> <input type="text" v-model='user.pwd'>
        <br>
        <span>确认密码</span> <input type="text" v-model='user.pass'>
        <br>
        <span>专业</span>
        <!-- 对于下拉菜单来说,option中间的内容是给用户看的,我们是通过value获取我们需要的值给变量 -->
        <select v-model='user.job'>          
            <option value="web工程师">web工程师</option>
            <option value="java工程师">java工程师</option>
            <option value="c++">c++</option>
        </select>
        <br>
        <span>爱好</span>
       <!-- 对于多选框来说,如果初始值是数组,则会把数据之间放到数组中,如果非数组类型,则会以布尔值展示结果 -->
       <input type="checkbox" value="唱歌" v-model='user.hobby'>唱歌
       <input type="checkbox" value="跳舞" v-model='user.hobby'>跳舞
       <input type="checkbox" value="打游戏" v-model='user.hobby'>打游戏
       <input type="checkbox" value="写代码" v-model='user.hobby'>写代码
       <br>
       <span>证书</span>
       <select v-model='user.zs' multiple>          
        <option value="计算机二级">计算机二级</option>
        <option value="英语四级">英语四级</option>
        <option value="英语六级">英语六级</option>
    </select>
    <br>
    <span>备注:</span>
    <textarea  cols="30" rows="10" v-model='user.text'></textarea>
    <br>
    <input type="checkbox" v-model='user.check'>我同意
    <br>
    <input type="submit" value="提交" @click = 'submit' :disabled='!user.check'>
    </div>
</body>
<script src="./vue.js"></script>
<script>
    let vm = new Vue({
        el:'#app',
        data:{
            user:{
                name:'zs',
                sex:'女',
                tel:'1111',
                pwd:'123',
                pass:'',
                job:'web工程师',
                hobby:['唱歌','跳舞'],
                zs:'',
                text:'哈哈',
                check:true
            }
        },
        methods: {
            submit(){
                if(this.user.pwd!=this.user.pass){
                    alert('两次输入的密码不一致')
                    return
                }
                console.log('发请求')
            }
        },
    })
</script>
```

 

2.v-for中key的使用

 
 
<ul>
            <!-- 总结:1.:key的取值必须是number类型或者是string类型,不能是对象 
                      2.v-for循环中的唯一值,只要使用循环就必须添加
            -->
            <li v-for='(item,index) in arr' :key='item.id'><input type="checkbox">{{item.name}}----{{item.id}}-------{{index}}</li>
        </ul>
        <button @click='add'>添加</button>


<script>
    let vm = new Vue({
        el:'#app',
        data:{
            arr:[
                {
                    id:1,
                    name:'丁有欣',
                },
                {
                    id:2,
                    name:'韩梦琪',
                },
                {
                    id:3,
                    name:'刘思皓',
                },
                {
                    id:4,
                    name:'杨传强',
                }
            ]
        },
        methods: {
            add(){
                const newArr = { id:5,name:'高衍锋'}
                this.arr.splice(2,0,newArr)
            }
        },
    })
</script>

 

 

 3.vue中的修饰符


1.事件修饰符

​   .stop .prevent  .self .once .capture(给谁加谁先出来)

2.键盘修饰符

```vue
<input type="text" @keydown.up = 'up' placeholder="向上">
<input type="text" @keydown.down = 'down' placeholder="向下">
<input type="text" @keydown.left = 'left' placeholder="向左">
<input type="text" @keydown.right = 'right' placeholder="向右">
<input type="text" @keydown.enter = 'enter' placeholder="回车">
```

3.鼠标修饰符

```vue
 <input type="text" @click.left = 'left' placeholder="左键">
 <input type="text" @click.middle = 'middle'  placeholder="滚轮">
 <input type="text" @click.right = 'right'  placeholder="右键">
```

### 4.生命周期

```vue
    <button>销毁</button>
    <div id="app">
        {{msg}}
        <div @click='add'>点击</div>
    </div>
</body>
<script src="./vue.js"></script>
<script>
    let vm = new Vue({
        data:{
            msg:'hello world!'
        },
        methods: {
           add(){
               console.log('我是vm方法');
           }
        },
        // 创建之前  什么都是undefined
        beforeCreate() {
            console.log('el:',this.$el);
            console.log('data:',this.$data);
        },
        // 创建完成, vue实例化初始化完成,但是el还是undefined
        created() {
            console.log('el:',this.$el);
            console.log('data:',this.$data);
        },
        // 挂载之前  已经找到挂载点,但是数据还未渲染 此时的el是div元素
        beforeMount() {
            console.log('挂载之前')
            console.log('el:',this.$el);
            console.log('data:',this.$data);
        },
        //挂载完成 数据加载完成
        mounted() {
            console.log('挂载完成')
            console.log('el:',this.$el);
            console.log('data:',this.$data);
        },
        // 更新之前  和更新之后的数据都是最新数据
        beforeUpdate() {
            console.log('更新之前')
            console.log('el:',this.$el);
            console.log('data:',this.$data);
        },
        // 更新完成
        updated() {
            console.log('更新完成')
            console.log('el:',this.$el);
            console.log('data:',this.$data);
        },
        // 销毁之前
        beforeDestroy() {
            console.log('销毁之前')
        },
        // 销毁完成
        destroyed() {
            console.log('销毁完成')
        },

    }).$mount('#app')
    // vm.$mount('#app')
    // document.querySelector('button').onclick = function(){
    //      vm.$mount('#app')
    // }
```



### 5.表单修饰符

```vue
 <div id="app">
         <input type="text" v-model='msg'>{{msg}}
        <br>
        <!-- .lazy   只有光标离开输入框,才会动态更新数据 -->
         <input type="text" v-model.lazy='str'>{{str}}
        <!-- .number 只要第一个值是数字类型,结果就是数字类型,不管第一个值后面是什么类型. 如果第一个值不是数字,那么就没有效果-->
         <input type="text" v-model.number='num' placeholder="请输入数字">
         <br>
         <!-- .trim   去除收尾空格  需要是用户输入的默认值-->
         <input type="text" v-model.trim='content'>
    </div>
</body>
<script src="./vue.js"></script>
<script>
    let vm = new Vue({
        el:'#app',
        data:{
            msg:'abc',
            str:'',
            num:1,
            content:'   abc   '
        }
    })
</script>
```

### 6.案例(字符串反转)

```vue
<div id="app">
        <div>{{msg}}</div>
        <button @click = 'reverse'>点击反转</button>
    </div>
</body>
<script src="./vue.js"></script>
<script>
    let vm = new Vue({
        el:'#app',
        data:{
           msg:'学IT到优就业'
        },
        methods: {
            reverse(){
                this.msg = this.msg.split('').reverse().join('')
            }
        },
    })
</script>
```

### 7.案例(表单的增删)

```vue
<div id="app">
       <h2>添加学员</h2>
       <div>
           <span>姓名</span><input type="text" v-model='name'>
       </div>
       <div>
        <span>年龄</span><input type="text" v-model='age'>
    </div>
    <button @click = 'add'>添加</button>
    <button @click = 'reset'>重置</button>

    <br>

    <h2>信息表</h2>
    <!-- border-collapse: collapse;设置边框为细线 -->
    <table border='1' cellpadding='0' cellspacing='0' style="border-collapse: collapse;width: 500px;">
        <tr>
            <th>序号</th>
            <th>姓名</th>
            <th>年龄</th>
            <th>操作</th>
        </tr>
        <tr v-for='(item,index) in arr' :key='item.id'>
            <td>{{index+1}}</td>
            <td>{{item.name}}</td>
            <td>{{item.age}}</td>
            <td>
                <button @click = 'del(index)'>删除</button>
            </td>
        </tr>
        <tr v-if='arr.length>0'>
            <td colspan='4'>
                <button @click = 'delAll'>全部删除</button>
            </td>
        </tr>
        <tr v-else>
            <td colspan='4'>暂无数据</td>
        </tr>
       
    </table>

    </div>
</body>
<script src="./vue.js"></script>
<script>
    let vm = new Vue({
        el:'#app',
        data:{
           name:'',
           age:'',
           arr:[
               {
                   id:1,
                   name:'刘坤',
                   age:18
               },
               {
                   id:2,
                   name:'冯丹',
                   age:19
               },
               {
                   id:3,
                   name:'乐乐',
                   age:18
               }
           ]
        },
        methods: {
            // 删除一个
            del(index){
                this.arr.splice(index,1)
            },
            // 全部删除
            delAll(){
                this.arr = []
            },
            // 添加
            add(){
                this.arr.push({
                    name:this.name,
                    age:this.age
                })
                this.reset()
            },
            // 重置
            reset(){
                this.name='',
                this.age=''
            }
        },
    })
</script>
```


博客园  ©  2004-2026
浙公网安备 33010602011771号 浙ICP备2021040463号-3