zoukankan      html  css  js  c++  java
  • vue.js查询天气

    // html文件

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <title>Document</title>
        <script src="./vue.js"></script>
        <script src="https://unpkg.com/axios/dist/axios.min.js"></script>
    </head>
    <body>
        <div id="app">
            <div class="serch_from">
                <input type="text" placeholder="请输入城市地址" @keyup.enter="search" class="search" v-model="city">
                <input type="button" value="搜索" @click="find">
            </div>
            <div class="hotkey">
                <a href="javascrpt:;" @click="changeCity('北京')">北京</a>
                <a href="javascrpt:;" @click="changeCity('上海')">上海</a>
                <a href="javascrpt:;" @click="changeCity('濮阳市')">濮阳市</a>

            </div>
           <ul class="weather_list">
               <li v-for="item in weatherList">
                   <div><span>{{item.type}}</span></div>
                   <div>
                       <p>{{item.low}}</p>
                       <p>{{item.high}}</p>
                   </div>
                   <div>
                       <span>{{item.date}}</span>
                   </div>
               </li>

           </ul>
        </div>
        <!--导入自己的js文件-->
        <script src="./js/main.js"></script>
        
    </body>
    </html>

      // js文件

    /*
    请求地址 http://wthrcdn.etouch.cn/weather_mini
    get请求
    请求参数 city 城市名
    */
    var vu=new Vue({
        el:"#app",
        data:{
            city:"",
            weatherList:[]

        },
        methods:{
            search:function(){
                console.log("天气查询");
                console.log(this.city);
                var that=this;//暂存this
                axios.get("http://wthrcdn.etouch.cn/weather_mini?city="+this.city).then(function(res){
                    console.log(res);
                    console.log(res.data.data.forecast)
                    that.weatherList=res.data.data.forecast;
                    console.log(this.weatherList)
                }).catch(function(err){
                    console.log(err)
                })
            },
            find:function(){
                console.log("搜索>>>>>>>>>>>>")
                var that=this;
                axios.get("http://wthrcdn.etouch.cn/weather_mini?city="+this.city).then(function(res){
                    console.log(res);
                    console.log(res.data.data.forecast)
                    that.weatherList=res.data.data.forecast;
                }).catch(function(err){
                    console.log(err)
                })
            },
            changeCity:function(city){
                console.log("点击城市>>>>>>>>>>>")
                this.city=city;
                var that=this;
                axios.get("http://wthrcdn.etouch.cn/weather_mini?city="+this.city).then(function(res){
                    console.log(res);
                    console.log(res.data.data.forecast)
                    that.weatherList=res.data.data.forecast;
                }).catch(function(err){
                    console.log(err)
                })

            }
        }
    })

      

  • 相关阅读:
    CSS复合选择器
    模块之shutil模块模块详解
    模块之sys模块详解
    模块之os模块详解
    map遍历
    java完美处理表情符
    死磕设计模式—建造者模式
    Java String getChars()方法
    Java知识系统回顾整理01基础06数组05复制数组
    Java知识系统回顾整理01基础06数组04增强型for循环
  • 原文地址:https://www.cnblogs.com/xianz666/p/14832313.html
Copyright © 2011-2022 走看看