Open Weather Api (Показать значок погоды) - PullRequest
0 голосов
/ 26 ноября 2018

Здравствуйте, сообщество снова к вам.Я играю с openweather API и создаю простое приложение погоды.У меня есть проблема, и я не могу показать значок погодных условий для каждого города.

Я выдвинул каждый идентификатор значка в новом массиве, и я пытаюсь показать его, используя 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>

1 Ответ

0 голосов
/ 26 ноября 2018

Вы не можете использовать curlies {{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>
...