初学Vue----案例二

案例一:随机笑话

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <script src="js/vue.js"></script>
</head>
<body>
<div id="app">
    <input type="button" value="每日一笑" @click="getjoke">
    <p>{{joke}}</p>
</div>
<script>
    new Vue({
        el: "#app",
        data: {
            joke: ""
        },
        methods: {
            getjoke: function () {
                var that = this;
                axios.get("https://autumnfish.cn/api/joke").then(function (value) {
                    that.joke = value.data;
                }, function (reason) {

                })
            }
        }
    })
</script>
</body>
</html>

运行结果:
在这里插入图片描述
案例二:查询天气

<!DOCTYPE html>
<html lang="en" xmlns:v-model="http://www.w3.org/1999/xhtml">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <script src="js/vue.js"></script>
    <script src="https://unpkg.com/axios/dist/axios.min.js"></script>
    <style type="text/css">
        * {
            padding: 0;
            margin: 0;
        }

        .search {
            width: 30%;
            margin: 10px auto;
            display: flex;
        }

        .search input {
            float: left;
            flex: 4;
            height: 30px;
            outline: none;
            border: 1px solid red;
            box-sizing: border-box;
            padding-left: 10px;
        }

        .search button {
            float: right;
            flex: 1;
            height: 30px;
            background-color: red;
            color: white;
            border-style: none;
            outline: none;
        }

        .search button i {
            font-style: normal;
        }

        .search button:hover {
            font-size: 16px;
            cursor: pointer;
        }
        #cc {
            margin: 0 auto;
            width: 100px;
        }
    </style>
</head>
<body>
<div id="app2">
    <div id="cc"><h2 style="font-family: 华文新魏">城市天气</h2></div>
    <div class="search">
        <input type="text" placeholder="请输入..." name="" id="" value="" @keyup.enter="searchWeather" v-model="city"/>
        <button @click="searchWeather"><i>搜索</i></button>
    </div>
    <ul style="width: 2000px;margin-left: 200px">
        <li v-for="item in weatherList" style="list-style: none;float: left;margin-left: 50px;">
            <p style="font-size: larger">&#12288;&#12288;&nbsp;{{item.type}}</p>
            <b style="font-size: smaller">{{item.low}}</b>~<b style="font-size: smaller">{{item.high}}</b>
            <p>&#12288;&nbsp;{{item.date}}</p>
        </li>
    </ul>
</div>
<script>
    new Vue({
        el:"#app2",
        data:{
          city:"",
          weatherList:[]
        },
        methods:{
            searchWeather:function () {
                var that = this;
                axios.get("https://wthrcdn.etouch.cn/weather_mini?city="+this.city).then(function (response) {
                        console.log(response.data.data.forecast);
                    that.weatherList = response.data.data.forecast;
                }).catch(function (reason) {

                })
            }
        }
    })
</script>
</body>
</html>

运行结果:
在这里插入图片描述

如果没有效果,换个浏览器即可

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值