Vue 相关

天气应用
Vue+axios
功能:
1.输入城市搜索相关天气功能(v-on enter v-model axios get that=this v-for [])
2.点击城市名切换该城市天气(this.city=city 调用 1 中函数)

代码:

<!DOCTYPE html>
<html lang="zh-CN">

<head>
    <meta charset="utf-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no,viewport-fit=cover">
    <title>天知道</title>
    <style>
        .wrap {
            width: 600px;
            margin: 20px auto 0;
        }
        
        .wrap header {
            text-align: center;
            font-size: 30px;
            color: blue;
            margin-bottom: 20px;
        }
        
        form {
            width: 100%;
        }
        
        form input {
            width: 78%;
            height: 50px;
            border: 1px solid blue;
        }
        
        form button {
            width: 20%;
            height: 50px;
            line-height: 50px;
            border: none;
            font-size: 18px;
            color: #fff;
            text-align: center;
            background-color: rgb(40, 136, 245);
        }
        
        footer {
            display: flex;
            margin-top: 30px;
        }
        
        li {
            list-style-type: none;
        }
    </style>
</head>

<body>
    <div class="wrap" id="sky">
        <header>天知道</header>
        <main>
            <form action="#">
                <input @keyup.enter="searchWeather" v-model="city" type="text" placeholder="请输入城市">
                <button>搜索</button>
                <div>
                    <a href="javascript:;" @click="changeCity('北京')">北京</a>
                    <a href="javascript:;" @click="changeCity('上海')">上海</a>
                    <a href="javascript:;" @click="changeCity('广州')">广州</a>
                    <a href="javascript:;" @click="changeCity('深圳')">深圳</a>
                </div>
            </form>
        </main>
        <footer>
            <ul>
                <li v-for="item in weatherList">
                    <div>
                        <strong>{{item.type}}</strong>
                        <span>{{item.low}}</span>
                        <span>{{item.high}}</span>
                    </div>
                </li>
            </ul>
        </footer>
    </div>
    <script src="https://cdn.jsdelivr.net/npm/vue@2.6.14/dist/vue.js"></script>
    <script src="https://unpkg.com/axios/dist/axios.min.js"></script>
    <script>
        let app = new Vue({
            el: "#sky",
            data: {
                city: '',
                weatherList: []
            },
            methods: {
                searchWeather: function() {
                    let that = this;
                    axios.get('http://wthrcdn.etouch.cn/weather_mini?city=' + this.city).then(function(response) {
                            that.weatherList = response.data.data.forecast
                        })
                        .catch(function(err) {})
                },
                changeCity: function(city) {
                    this.city = city;
                    this.searchWeather();
                }
            }
        })
    </script>
</body>

</html>
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • vue面试题 一、vue优点 1.轻量级框架:只关注视图层,是一个构建数据的视图集合,大小只有几十kb; 2.简单...
    没糖_cristalle阅读 5,497评论 0 3
  • 1、对于Vue是一套渐进式框架的理解 2、vue.js的两个核心是什么? 3、请问 v-if 和 v-show 有...
    Mylovesunshine阅读 2,948评论 0 0
  • 1.v-if和v-show区别: v-if --> 创建、删除 (没有元素) 真正的条件渲染。切换开销较高 ...
    蛋黄呆呆的阅读 4,560评论 0 1
  • 1. 组件的data为什么必须是函数? 组件中的 data 写成一个函数,数据以函数返回值形式定义,这样每复用一次...
    郭先生_515阅读 4,549评论 0 12
  • 1.Vue的数据双向绑定如何实现?Vue的数据双向绑定如何实现? v-model指令或者.sync修饰 2.Vue...
    骑着蜗牛逛妓院阅读 9,227评论 1 11