【发布时间】:2020-09-01 20:53:46
【问题描述】:
我正在尝试从 api 获取温度数据。我无法让它工作。我做错了什么?在显示结果的 div 中是否需要 v-for 或其他内容?
new Vue({
el: "#app",
data () {
return {
api_key: 'adf173dfdcd1a6aea78ba12651a19177',
base_url: 'https://api.openweathermap.org/data/2.5/',
weather: {},
query: ''
}
},
methods: {
grabWeather() {
axios.get(`api.openweathermap.org/data/2.5/weather?id=${this.query}&appid=${this.api_key}`)
.then(response => {
this.weather = response.data;
})
}
}
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/axios/0.20.0/axios.min.js"></script>
<div id="app">
<h2>Temperature:</h2>
<input type="text" v-model="query">
<div>
{{weather.name}}
{{weather.temp}}
</div>
<button @click="grabWeather">
Grab
</button>
</div>
【问题讨论】:
-
您不应在帖子中包含 API 密钥。