前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >vuejs小例子之天气查询

vuejs小例子之天气查询

作者头像
西西嘛呦
发布2020-08-26 22:42:00
4920
发布2020-08-26 22:42:00
举报
文章被收录于专栏:数据分析与挖掘

看一个例子:

代码语言:javascript
复制
<html>

<head>
  <script src="https://unpkg.com/axios/dist/axios.min.js"></script>
  <script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
</head>

<body>
  <div id="app">
    <input type="text" v-model="city" @keyup.enter="getWeathers">
    <button @click="getWeathers">查询</button>
    <br>
    <br>
    <a href="#" @click="changeCity('北京')">北京</a>
    <a href="#" @click="changeCity('武汉')">武汉</a>
    <a href="#" @click="changeCity('上海')">上海</a>
    <a href="#" @click="changeCity('深圳')">深圳</a>
    <ul>
      <li v-for="(item, index) in weathers">
        {{item.type}} {{item.low}} {{item.high}} {{item.date}}
      </li>
    </ul>
  </div>
  <script>
    var vue = new Vue({
      el: "#app",
      data: {
        city: '',
        weathers: [],
      },
      methods: {
        getWeathers: function () {
          var that = this
          axios.get('http://wthrcdn.etouch.cn/weather_mini?city=' + this.city)
            .then(function (response) {
              that.weathers = response.data.data.forecast;
            }, function (err) {

            })
        },
        changeCity: function (city) {
          this.city = city;
          this.getWeathers();
        },
      },
    })
  </script>
</body>

</html>

效果:

在输入框中输入城市,例如武汉,回车或者点击按钮查询:

会显示相关信息。

点击下面的四个城市:

也会显示相关信息。

说明:使用v-model,v-on点击事件,v-on回车事件,v-for遍历数据,axios发送请求。

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2020-02-18 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档