【问题标题】:Open Weather Api (Show weather icon)Open Weather Api(显示天气图标)
【发布时间】:2019-04-27 21:08:06
【问题描述】:

您好社区再次回复您。我正在使用 openweather API,并且正在构建一个简单的天气应用程序。我有一个问题,我无法显示每个城市的天气状况图标。

我将每个图标 id 推送到 newArray 中,并尝试使用 url 显示它,但没有运气http://openweathermap.org/img/w/10d.png

有什么建议吗?提前致谢

var app = new Vue({
	el: "#app",
	data: {

		test: [],
		image: [],
		newArray:[],
		message: "",
		url: "",
		
	},
	created: function () {
		this.getData();
		this.cities();
	
		},
        methods: {
		getData: function () {
			var fetchConfig =
				fetch("https://api.myjson.com/bins/i8run", {
					method: "GET",
					headers: new Headers({})
				}).then(function (response) {
					if (response.ok) {
						return response.json();
					}
				}).then(function (json) {
					console.log("My json", json)

					app.test = json.list;
					console.log(app.test);
      				app.pushAnArr();
			

				})
				.catch(function (error) {
					console.log(error);
				})
		},
			
		cities: function () {
			var fetchConfig =
				fetch("https://pixabay.com/api/?key=10772849-8270b213e517e422b036ea0fd&q=city", {
					method: "GET",
					headers: new Headers({})
				}).then(function (response) {
					if (response.ok) {
						return response.json();
					}
				}).then(function (json) {
					console.log("My json", json)

					app.image = json.hits;
					console.log(app.image);
				

				})
				.catch(function (error) {
					console.log(error);
				})
		},
			
	 pushAnArr: function(){
		for(var i=0; i<app.test.length; i++){
			
			app.newArray.push(app.test[i].weather[0].icon);
		 
			
		}
		 			console.log(app.newArray); 

		
	 }
			
			
	}
})

 
<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>Document</title>
  <link rel="stylesheet" type="text/css" href="style.css">
</head>
<body>
    <div id="app">
		 <div v-for="item in newArray" :key="item.id" class="thecard">
                      {{item}}
       <img src="http://openweathermap.org/img/w/item.png" >
					   </div>
                
	</div>

		


		
	<script src="https://unpkg.com/vue/dist/vue.js"></script>
    <script src="main.js"></script>
</body>
</html>

【问题讨论】:

  • 您每次都使用静态 URL http://openweathermap.org/img/w/item.png。也许你想把你的{{item}} 占位符也放在那里……?
  • 你肯定是想显示 src="openweathermap.org/img/w/item.png",不是吗?

标签: javascript html api vue.js weather


【解决方案1】:

您不能在属性中使用卷曲 {{item}}

用途:

<img v-bind:src="'http://openweathermap.org/img/w/' + item + '.png' "  />

var app = new Vue({
	el: "#app",
	data: {

		test: [],
		image: [],
		newArray:[],
		message: "",
		url: "",
		
	},
	created: function () {
		this.getData();
		this.cities();
	
		},
        methods: {
		getData: function () {
			var fetchConfig =
				fetch("https://api.myjson.com/bins/i8run", {
					method: "GET",
					headers: new Headers({})
				}).then(function (response) {
					if (response.ok) {
						return response.json();
					}
				}).then(function (json) {
					console.log("My json", json)

					app.test = json.list;
					console.log(app.test);
      				app.pushAnArr();
			

				})
				.catch(function (error) {
					console.log(error);
				})
		},
			
		cities: function () {
			var fetchConfig =
				fetch("https://pixabay.com/api/?key=10772849-8270b213e517e422b036ea0fd&q=city", {
					method: "GET",
					headers: new Headers({})
				}).then(function (response) {
					if (response.ok) {
						return response.json();
					}
				}).then(function (json) {
					console.log("My json", json)

					app.image = json.hits;
					console.log(app.image);
				

				})
				.catch(function (error) {
					console.log(error);
				})
		},
			
	 pushAnArr: function(){
		for(var i=0; i<app.test.length; i++){
			
			app.newArray.push(app.test[i].weather[0].icon);
		 
			
		}
		 			console.log(app.newArray); 

		
	 }
			
			
	}
})

 
<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>Document</title>
  <link rel="stylesheet" type="text/css" href="style.css">
</head>
<body>
    <div id="app">
		 <div v-for="item in newArray" :key="item.id" class="thecard">
                      {{item}}
       <img v-bind:src="'http://openweathermap.org/img/w/' + item + '.png' "  />
					   </div>
                
	</div>

		


		
	<script src="https://unpkg.com/vue/dist/vue.js"></script>
    <script src="main.js"></script>
</body>
</html>

【讨论】:

  • 非常感谢。我没有在我的代码中使用 {{}} 只是一种测试,您的解决方案有效。 :) :)
猜你喜欢
  • 1970-01-01
  • 2020-05-19
  • 1970-01-01
  • 2013-07-06
  • 1970-01-01
  • 1970-01-01
  • 2020-09-09
  • 1970-01-01
  • 2017-10-25
相关资源
最近更新 更多