vue学习笔记

v-on

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width,initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>vue太难了</title>
</head>

<body>
    <div id="app">
        <input type="button" value="v-on指令" v-on:click="doIt">
        <input type="button" value="v-on简写" @click="doIt">
        <input type="button" value="双击事件" @dblclick="doIt">
        <h2 @click="changeFood">{{food}}</h2>
    </div>

    <script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
    <script>
     var app=new Vue({
         el:"#app",
         data:{
             food:"西兰花炒蛋"
         },
         methods:{
             doIt:function (){
                 alert("做It");//alert是js的函数,弹窗
             },
             changeFood:function (){
                 this.food+="好好吃!"
             }
         },
     })
    </script>

</body>
</html>

计数器的实现

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>计数器</title>
    <link rel="stylesheet" href="./css/index.css">
    <!--偷偷自己加的样式,属于CSS的内容-->
</head>

<body>
    <div id="app">
        <div class="input-num">
            <button @click="sub">-</button>
            <span>{{num}}</span>
            <button @click="add">+</button>

        </div>
    </div>
    <script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
    <script>
        var app=new Vue({
            el:"#app",
            data:{
                num:1
            },
            methods:{
                add:function (){
                    if(this.num<10)
                    {
                        this.num++;
                    }else{
                        alert("别点了,最大了")
                    }

                },
                sub:function (){
                    if(this.num>0)
                    {
                       this.num--;
                    }else{
                        alert("别点了,最小了")
                    }
                }
            }
        })
    </script>

</body>
</html>

v-show指令

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>v-show 指令</title>
</head>
<body>
    <div id="app">
        <input type="button" value="切换显示状态" @click="changeIsShow">
        <input type="button" value="累加年龄" @click="addAge">
        <img v-show="isShow" src="//www.runoob.com/images/logo.png" alt="">
        <!--//本地插入图片有点小难-->
        <img v-show="age>=18" src="//www.runoob.com/images/logo.png" alt="">
    </div>
<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
<script>
    var app = new Vue({
        el:"#app",
        data:{
            isShow:false,
            age:17
        },
        methods:{
            changeIsShow:function (){
                this.isShow = !this.isShow;
            },
            addAge:function (){
                this.age++;
            }
        }
    })
</script>

</body>
</html>

v-if

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>v-if</title>
</head>
<body>
    <div id="app">
        <input type="button" value="切换显示" @click="toggleIsShow">
        <p v-if="isShow">西兰花</p>
        <p v-show="isShow">西兰花-v-show</p>
        <h2 v-if="temperature>=35">热死了</h2>
    </div>
<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
<script>
    var app=new Vue({
        el:"#app",
        data:{
            isShow:false,
            temperature:45
        },
        methods:{
            toggleIsShow:function (){
                this.isShow = !this.isShow;
            }
        }
    })
</script>

</body>
</html>

v-bind

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>v-bind</title>
    <style>
        .active{
            border: 1px solid red;
        }
    </style>
</head>
<body>
    <div id="app">
        <img v-bind:src="imgSrc" alt="">
        <br>
        <img :src="imgSrc" alt="" :title="imgTitle+'!!!'" :class="isActive?'active':''" @click="toggleActive">
        <br>
        <img :src="imgSrc" alt="" :title="imgTitle" :class="{active:isActive}" @click="toggleActive">
    </div>
    <script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
    <script>
        var app=new Vue({
            el:"#app",
            data:{
                imgSrc:"//www.runoob.com/images/logo.png",
                imgTitle:"菜鸟",
                isActive:false
            },
            methods:{
                toggleActive:function (){
                    this.isActive=! this.isActive;
                }
            }
        })
    </script>

</body>
</html>

有个图片切换的作业,因为没有CSS的样式 ,选择性放弃

v-for指令

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>v-for</title>
</head>
<body>
    <div id="app">
        <input type="button" value="添加数据" @click="add">
        <input type="button" value="删除数据" @click="remove">
        <ul>
            <li v-for="(it,index) in arr">
                {{index+1}} 黑马程序猿校区:{{it}}
            </li>
        </ul>
        <h2 v-for="item in vegetable" v-bind:title="item.name">
            {{item.name}}
        </h2>
    </div>

<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
    <script>
        var app = new Vue({
            el:"#app",
            data:{
                arr:["北京","上海","广州","深圳"],
                vegetable:[
                    {name:"西兰花炒蛋"},
                    {name:"蛋炒西兰花"}
                    ]},
            methods:{
                add:function (){
                    this.vegetable.push({name:"兰菜炒蛋"});
                },
                remove:function (){
                    this.vegetable.shift();
                }
            }

        })
    </script>

</body>
</html>

v-on补充

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>v-on</title>
</head>
<body>
    <div id="app">
        <input type="button" value="点击" @click="doIt(666,'老铁')">
        <input type="text" @keyup.enter="sayHi">
    </div>
    <script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
    <script>
        var app=new Vue({
            el:"#app",
            methods:{
                doIt:function (p1,p2){
                    console.log("做it");
                    console.log(p1);
                    console.log(p2);
                },
                sayHi:function (){
                    alert("吃了没");
                }
            }
        })
    </script>


</body>
</html>

v-model 

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>v-model</title>
</head>
<body>
    <div id="app">
        <input type="button" value="修改message" @click="setM">
        <input type="text" v-model="message" @keyup.enter="getM">
        <h2>{{message}}</h2>
    </div>
    <script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
    <script>
        var app=new Vue({
            el:"#app",
            data:{
                message:"黑马程序猿"
            },
            methods:{
                getM:function (){
                    alert(this.message);
                },
                setM:function (){
                    this.message ="苦丁鱼";
                }


            }
        })

    </script>

</body>
</html>

axios

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>axios基本使用</title>
</head>
<body>
    <input type="button" value="get请求" class="get">
    <input type="button" value="post请求" class="post">

    <script src="https://unpkg.com/axios/dist/axios.min.js"></script>
    <script>
        document.querySelector(".get").onclick = function (){
            axios.get("https://autumnfish.cn/api/joke/list?num=6")
            .then(function (response){
                console.log(response);
            },function(err){
                console.log(err);
            })
        }
        document.querySelector(".post").onclick=function (){
            axios.post("https://autumnfish.cn/api/user/reg",{username:"侯开宇"})
            .then(function (response){
                console.log(response);
            })
        }
    </script>

</body>
</html>

axios+vue

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>vue+axios</title>
</head>
<body>
    <div id="app">
        <input type="button" value="获取笑话" @click="getJoke">
        <p>{{joke}}</p>
    </div>
    <script src="https://unpkg.com/axios/dist/axios.min.js"></script>
    <script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
    <script>
        var app=new Vue({
            el:"#app",
            data:{
                joke:"很好笑的笑话"
            },
            methods:{
                getJoke:function (){
                    var that =this;
                    axios.get("https://autumnfish.cn/api/joke").then(
                        function (response){
                        console.log(response.data);
                        that.joke=response.data;
                }
                    )
                }
            }
        })
    </script>

</body>
</html>

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值