Vue基础

模板语法

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>初识vue</title>
    <script type = "text/javascript" src="../js/vue.js"></script>
</head>
<body>
    <div id="root">
       <h1>插值语法</h1>
       <h3>hello  {{name}}</h3>

       <h1>指令语法</h1>
       <a v-bind:href="school.url.toUpperCase()" x="hello"> 点我去{{school.name}}学习</a>
       <a :href="school.url">点我去{{school.name}}</a>
    </div>


    <script type="text/javascript">
        Vue.config.productionTip = false
    new Vue({
        el:'#root',
        data:{
            name:'aa',
            school:{
                name:'尚硅谷',
                url:'http://www.baidu.com'
            }
            
        }
    })
    </script>
</body>
</html>

数据绑定

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>数据绑定</title>
    <script type = "text/javascript" src="../js/vue.js"></script>
</head>
<body>
    <div id="root">
        单向数据绑定:<input type="text" :value="name"> <br/>
        双向数据绑定:<input type="text" v-model="name"> <br/>
    </div>

    
    <script type="text/javascript">
        Vue.config.productionTip = false
    new Vue({
        el:'#root',
        data:{
            name:'aa'
        }
    })
    
    </script>
</body>
</html>

el与data的两种写法

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>两种写法</title>
    <script type = "text/javascript" src="../js/vue.js"></script>
</head>
<body>
    <div id="root">
        <h1>你好 {{name}}</h1>
    </div>

    
    <script type="text/javascript">
        Vue.config.productionTip = false
    // const v = new Vue({
    //     //el:'#root', //第一种写法
    //     data:{
    //         name:'aa'
    //     }
    // })
    // console.log(v)
    // v.$mount('#root')   

    new Vue({
        el:'#root',
        // data:function(){
        //     return{
        //         name:'sss'
        //     }
        // }
        data(){
            console.log('@@@',this)
            return{
                name:'sss'
            }
        }
    })
    </script>
</body>
</html>

1事件的基本使用

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>两种写法</title>
    <script type = "text/javascript" src="../js/vue.js"></script>
</head>
<body>
    <div id="root">
        <h1>你好 {{name}}</h1>
        <button v-on:click = "showInfo">点我提示信息</button>
        <button @click = "showInfo1">点我提示信息</button>
    </div>

    
    <script type="text/javascript">
        Vue.config.productionTip = false
    

    new Vue({
        el:'#root',
        data:{
            name:'aa'
        },
        methods:{
            showInfo(){
                alert('22222')
            },
            showInfo1(){
                alert('22222')
            }
        }
    })
    </script>
</body>
</html>

08计算属性

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>两种写法</title>
    <script type = "text/javascript" src="../js/vue.js"></script>
</head>
<body>
    <div id="root"><input type="text" v-model:value="firstName"><br/><input type="text" v-model:value="lastName"> <br/>
       姓名: <span>{{firstName}}--{{lastName}}</span>
    </div>

    
    <script type="text/javascript">
        Vue.config.productionTip = false
    

    new Vue({
        el:'#root',
        data:{
            firstName:'qi',
            lastName:'jun'
        }
    })
    </script>
</body>
</html>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>两种写法</title>
    <script type = "text/javascript" src="../js/vue.js"></script>
</head>
<body>
    <div id="root"><input type="text" v-model="firstName"><br/><input type="text" v-model="lastName"> <br/>
       姓名: <span>{{fullName()}}</span>
    </div>

    
    <script type="text/javascript">
        Vue.config.productionTip = false
    

    new Vue({
        el:'#root',
        data:{
            firstName:'qi',
            lastName:'jun'
        },
        methods:{
            fullName(){
                return this.firstName + this.lastName
            }
        }
    })
    </script>
</body>
</html>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>两种写法</title>
    <script type = "text/javascript" src="../js/vue.js"></script>
</head>
<body>
    <div id="root"><input type="text" v-model="firstName"><br/><input type="text" v-model="lastName"> <br/>
       姓名: <span>{{fullName}}</span> <br/>
       姓名: <span>{{fullName}}</span> <br/>
       姓名: <span>{{fullName}}</span> <br/>
       姓名: <span>{{fullName}}</span> <br/>
    </div>

    
    <script type="text/javascript">
        Vue.config.productionTip = false
    

    new Vue({
        el:'#root',
        data:{
            firstName:'qi',
            lastName:'jun',
        },
        computed:{
            fullName:{
                get(){
                    return this.firstName + '-' + this.lastName
                },
                set(value){
                    const arr = value.split('-')
                    this.firstName = arr[0]
                    this.lastName = arr[1]
                }
            }
        }
    })
    </script>
</body>
</html>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>两种写法</title>
    <script type = "text/javascript" src="../js/vue.js"></script>
</head>
<body>
    <div id="root"><input type="text" v-model="firstName"><br/><input type="text" v-model="lastName"> <br/>
       姓名: <span>{{fullName}}</span> <br/>
       姓名: <span>{{fullName}}</span> <br/>
       姓名: <span>{{fullName}}</span> <br/>
       姓名: <span>{{fullName}}</span> <br/>
    </div>

    
    <script type="text/javascript">
        Vue.config.productionTip = false
    

    new Vue({
        el:'#root',
        data:{
            firstName:'qi',
            lastName:'jun',
        },
        computed:{
            fullName(){
                return this.firstName + '-' + this.lastName
            }
             
        }
    })
    </script>
</body>
</html>

监视属性

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>两种写法</title>
    <script type = "text/javascript" src="../js/vue.js"></script>
</head>
<body>
    <div id="root">
        <h1>今天的天气很{{info}}</h1>
        <button @click="changeWeather">切换</button>
    </div>

    
    <script type="text/javascript">
        Vue.config.productionTip = false
    
    new Vue({
        el:'#root',
        data:{
            isHot:true
        },
        computed:{
            info(){
            return this.isHot ? '炎热' :'凉爽'
            }
        },
        methods: {
            changeWeather(){
                this.isHot = !this.isHot
            }
        },
    })
   
    </script>
</body>
</html>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>两种写法</title>
    <script type = "text/javascript" src="../js/vue.js"></script>
</head>
<body>
    <div id="root">
        <h1>今天的天气很{{info}}</h1>
        <button @click="changeWeather">切换</button>
    </div>

    
    <script type="text/javascript">
        Vue.config.productionTip = false
    
    const vm = new Vue({
        el:'#root',
        data:{
            isHot:true
        },
        computed:{
            info(){
            return this.isHot ? '炎热' :'凉爽'
            }
        },
        methods: {
            changeWeather(){
                this.isHot = !this.isHot
            }
        },
        // watch:{
        //     isHot:{
        //         immediate:true,//初始化时让handler调用一下
        //         handler(newValue,oldValue){
        //             console(newValue,oldValue)
        //         }
        //     },
        //     info:{
        //         immediate:true,//初始化时让handler调用一下
        //         handler(newValue,oldValue){
        //             console(newValue,oldValue)
        //         }
        //     }
        // }
    })
   vm.$watch('isHot',{
    immediate:true,
    handler(newValue,oldValue){

    }
   })
    
    
    </script>
</body>
</html>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>两种写法</title>
    <script type = "text/javascript" src="../js/vue.js"></script>
</head>
<body>
    <div id="root">
        <h1>今天的天气很{{info}}</h1>
        <button @click="changeWeather">切换</button>

        <hr/>

        <h3>a的值是{{numbers.a}}</h3>
        <button @click="numbers.a++">点我让a++</button>
        <h3>b的值是{{numbers.b}}</h3>
        <button @click="numbers.b++">点我让b++</button>
    </div>

    
    <script type="text/javascript">
        Vue.config.productionTip = false
    
    const vm = new Vue({
        el:'#root',
        data:{
            isHot:true,
            numbers:{
                a:1,
                b:1
            }
        },
        computed:{
            info(){
            return this.isHot ? '炎热' :'凉爽'
            }
        },
        methods: {
            changeWeather(){
                this.isHot = !this.isHot
            }
        },
        watch:{
            numbers:{
                deep:true,
                handler(){
                    console.log('numbers改变了')
                }
            }
            
        }
    })

    
    
    </script>
</body>
</html>

12列表渲染

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>列表渲染</title>
    <script type = "text/javascript" src="../js/vue.js"></script>
</head>
<body>
    <div id="root">
        <h2>人员列表</h2>
        <ul>
           <li  v-for="p in persons" :key="p.id">
                {{p.name}} -- {{p.age}}
           </li> 
        </ul>
        <h2>汽车信息</h2>
        <ul>
           <li  v-for="(value,k) in car" :key="k">
                {{k}} -- {{value}}
           </li> 
        </ul>



    </div>
    <script type="text/javascript">
        Vue.config.productionTip = false
        new Vue({
            el:'#root',
            data:{
                persons:[
                    {id:'1',name:'xxx',age:'15'},
                    {id:'2',name:'xxxx',age:'11'},
                    {id:'3',name:'xxxxx',age:'111'}
                ],
                car:{
                    name:'xx',
                    price:'2222'
                }
            }
        })



    </script>
</body>
</html>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>列表渲染</title>
    <script type = "text/javascript" src="../js/vue.js"></script>
</head>
<body>
    <div id="root">
        <input v-model="keyWord" type="text" placeholder="请输入名字">
        <ul>
            <li v-for="(p,index) of filPersons" :key="index">
                {{p.name}}--{{p.age}}-{{p.sex}}
            </li>

        </ul>



    </div>
    <script type="text/javascript">
        Vue.config.productionTip = false
        //用watch实现
        // new Vue({
        //     el:'#root',
        //     data:{
        //         keyWord:'',
        //         persons:[
        //             {id:'001',name:'马东',age:'22',sex:'女'},
        //             {id:'002',name:'六三',age:'32',sex:'男'}
        //         ],
        //         filPersons:[]
        //     },
        //     watch:{
        //         keyWord:{
        //             immediate:true,
        //             handler(val){
        //                 this.filPersons = this.persons.filter((p)=>{
        //                 return p.name.indexOf(val)!== -1
        //             })
        //             }
                   
        //         }
        //     }
            
        // })

        //用computed实现
        new Vue({
            el:'#root',
            data:{
                keyWord:'',
                persons:[
                    {id:'001',name:'马东',age:'22',sex:'女'},
                    {id:'002',name:'六三',age:'32',sex:'男'}
                ]
            },
            computed:{
                filPersons(){
                    return  this.persons.filter((p)=>{
                        return p.name.indexOf(this.keyWord)!== -1
                    })
                }
            }
        })

    </script>
</body>
</html>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>列表渲染</title>
    <script type = "text/javascript" src="../js/vue.js"></script>
</head>
<body>
    <div id="root">
        <input v-model="keyWord" type="text" placeholder="请输入名字">
        <button @click="sortType =1">升序</button>
        <button @click="sortType =2">降序</button>
        <button @click="sortType =0">原顺序</button>
        <ul>
            <li v-for="(p,index) of filPersons" :key="index">
                {{p.name}}--{{p.age}}-{{p.sex}}
            </li>

        </ul>



    </div>
    <script type="text/javascript">
        Vue.config.productionTip = false
        
        //用computed实现
        new Vue({
            el:'#root',
            data:{
                keyWord:'',
                persons:[
                    {id:'001',name:'马东',age:'22',sex:'女'},
                    {id:'002',name:'六三',age:'32',sex:'男'}
                ],
                sortType:0
            },
            computed:{
                filPersons(){
                    const arr =  this.persons.filter((p)=>{
                        return p.name.indexOf(this.keyWord)!== -1
                    })
                    if(this.sortType){
                        arr.sort((p1,p2) =>{
                            return this.sortType > 1 ? p2.age-p1.age : p1.age-p2.age
                        })
                    }
                    return arr
                }
            }
        })

    </script>
</body>
</html>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值