简体   繁体   English

无法使用 Vue.js 从 opendweathermap 获取天气数据

[英]Can't get weather data from opendweathermap using Vue.js

I want to create a simple weather report website using Vue.js, I just learned this framework and had accessed public data before.我想用Vue.js创建一个简单的天气预报网站,我刚刚学习了这个框架,之前访问过公共数据。 But this time I am stuck.但这一次我被困住了。

There are two versions of methods I have tried to get data.我尝试获取数据的方法有两种版本。

 var app = new Vue({ el: "#weather", data: { city: '', weather:[], date: new Date().toDateString(), greeting:'' }, methods: { //method 1 getData: function () { var city = this.city $.getJSON("http://api.openweathermap.org/data/2.5/weather?q=" + city + "&units=metric&lang=en&appid=a495404234abce9b5830b1e8d20e90a6", function (data) { console.log(data) }); }, //method 2 getData: function () { $("#search").keypress(function (e) { if (e.which == 13) { var city = $("#search").val(); if (city != " ") { var url = "http://api.openweathermap.org/data/2.5/weather?q=" + city + "&units=metric&lang=en&appid=a495404234abce9b5830b1e8d20e90a6"; console.log(url); } $.getJSON(url, function (data) { this.weather = data.weather; console.log(data); this.returnGreeting(); }) } }) }, } } });
 <div class="container" id="weather"> <h1>Weather Pro</h1> <div class="float-left"><h2>{{date}}</h2></div> <div class="float-right" ><h3 id="time"></h3></div> <p>{{greeting}}</p> <div class="input-group"> <form> <input v-model="city" class='searchbar transparent' id='search' type='text' placeholder=' enter city' /> <input id='button' @click="getData" type="button" value='GO' /> </form> </div> {{city}} <div class="panel"> <div class="panel-body" v-for="d in data"> <p> {{data}} </p> </div> <ul class="list-group list-group-flush"> <!-- <li class="list-group-item">{{data.weather[0].main}}</li> <li class="list-group-item">{{data.weather[0].description}}</li> --> </ul> </div> </div> <!-- vue --> <script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script> <!-- jQuery library --> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

I got an error :我收到一个错误:

[Vue warn]: Property or method "data" is not defined on the instance but referenced during render. [Vue 警告]:属性或方法“数据”未在实例上定义,但在渲染期间被引用。 Make sure that this property is reactive, either in the data option, or for class-based components, by initializing the property.通过初始化该属性,确保该属性是反应性的,无论是在数据选项中,还是对于基于类的组件。 See: https://v2.vuejs.org/v2/guide/reactivity.html#Declaring-Reactive-Properties .请参阅: https ://v2.vuejs.org/v2/guide/reactivity.html#Declaring-Reactive-Properties。

Consider data to be your model.data视为您的模型。 Don't reference data directly in your view, reference properties that are on the model instead.不要直接在视图中引用data ,而是引用模型上的属性。

So instead of <div>{{data.city}}</div> use <div>{{city}}</div>所以代替<div>{{data.city}}</div>使用<div>{{city}}</div>

 var app = new Vue({ el: "#weather", data() { return { city: '', weather: [], date: new Date().toDateString(), greeting: '' }; }, methods: { getData() { fetch("http://api.openweathermap.org/data/2.5/weather?q=" + this.city + "&units=metric&lang=en&appid=a495404234abce9b5830b1e8d20e90a6") .then(res => res.json()) .then(data => { this.weather = data.weather; }); } } });
 <div class="container" id="weather"> <h1>Weather Pro</h1> <div class="float-left"> <h2>{{date}}</h2> </div> <div class="float-right"> <h3 id="time"></h3> </div> <p>{{greeting}}</p> <div class="input-group"> <form> <input v-model="city" class='searchbar transparent' id='search' type='text' placeholder=' enter city' /> <input id='button' @click="getData" type="button" value='GO' /> </form> </div> {{city}} <div class="panel"> <div class="panel-body" v-for="d in weather"> <p> {{d}} </p> </div> <ul class="list-group list-group-flush"></ul> </div> </div> <!-- vue --> <script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.min.js"></script> <!-- jQuery library --> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

looks like zero beat me to it, but here's a version using jquery call看起来零击败了我,但这是一个使用 jquery 调用的版本

the issue is, as mentioned in comment, that data.data is not defined.正如评论中提到的,问题是data.data没有定义。 so define data inside data, and assign result to this.data .所以在数据中定义数据,并将result分配给this.data However, because it's inside a function and the scope changes, you need to store scope using var that = this and use that.data = data to assign result但是,因为它在函数内部并且范围发生了变化,所以您需要使用var that = this存储范围并使用that.data = data来分配结果

dom:域名:

<div class="container" id="weather">
  <h1>Weather Pro</h1>
  <div class="float-left"><h2>{{date}}</h2></div>
  <div class="float-right" ><h3 id="time"></h3></div>
  <p>{{greeting}}</p>

  <div class="input-group">
    <form>
      <input v-model="city" class='searchbar transparent' id='search' type='text' placeholder='      enter city' />

      <input id='button' @click="getData" type="button" value='GO' />

    </form>
  </div>

  {{city}}
  <div class="panel">

    <div class="panel-body" v-for="d in data">
      <p>
        {{d}}
      </p>
    </div>
    <ul class="list-group list-group-flush">
    </ul>

  </div>
</div>

<!-- jQuery library -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

Script:脚本:

var app = new Vue({
  el: "#weather",
  data: {
    city: '',
    weather:[],
    date: new Date().toDateString(),
    greeting:'',
    data: null,

  },

  methods: {
    //method 1
    getData: function () {
      var that = this;
      var city = this.city
      console.log('getData')
      $.getJSON("https://api.openweathermap.org/data/2.5/weather?q=" + city + "&units=metric&lang=en&appid=a495404234abce9b5830b1e8d20e90a6", 
                function (data) {
        console.log(data)
        that.data = data;
      });

    },
  }
});

Here is an example fiddle .这是一个示例小提琴

I found out what caused the issues:我发现了导致问题的原因:

  1. I need to define data in data, as I reference data directly in my html page, but this is optional.我需要在数据中定义数据,因为我直接在我的 html 页面中引用数据,但这是可选的。
  2. Turns out there is a slim jQuery version from bootstrap that overrides the min jQuery.原来有一个来自 bootstrap 的苗条 jQuery 版本,它覆盖了 min jQuery。 And $.getJSON() needs min jQuery.并且$.getJSON()需要最小的 jQuery。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM