vue 项目 web端如何实现天气预报1

自动定位天气信息

1.拿到当前电脑ip
2.高德接口获取ip的地址
3.通过和风天气获取城市id
4.通过id获取实时天气

高德API:https://lbs.amap.com/api/webservice/guide/api/ipconfig
和风天气:https://dev.qweather.com/docs/api/weather/weather-now/

拿到当前IP

一、在index.js里引入

<script src="http://pv.sohu.com/cityjson?ie=utf-8"></script>

二、获取IP

const ip = returnCitySN["cip"]
通过IP定位当前城市
 this.$axios.get(`https://restapi.amap.com/v3/ip?ip=${ip}&key=你的key`).then(res => {
 console.log(res)
 }
通过地址拿到和风天气对应的ID
this.$axios.get(`https://geoapi.qweather.com/v2/city/lookup?location=${res.data.city}&key=你的key`).then(res => {
	console.log(res)
}
通过ID和实时天气
this.$axios.get(`https://devapi.qweather.com/v7/weather/now?location=${res.data.location[0].id}&key=你的key`).then(res => {
 console.log(res)
})
Vue CLI是一个用于快速构建基于Vue.js应用的脚手架工具,它可以帮助开发者更方便地初始化、配置和管理Vue项目。要使用Vue CLI实现一个简单的天气预报应用,你可以按照以下步骤操作: 1. **安装Vue CLI**: 使用npm或yarn全局安装Vue CLI: ``` npm install -g @vue/cli ``` 2. **创建新项目**: 运行`vue create weather-app` 创建一个新的Vue项目,选择默认选项即可。 3. **选择组件化结构**: Vue CLI会生成一个基本的文件结构,包括`src/components`目录,这里可以创建一个名为`Weather.vue`的组件来显示天气信息。 4. **编写Weather组件**: 在`Weather.vue`文件中,使用`axios`或`fetch`等库从API获取天气数据,然后展示城市名、温度、天气状况等信息。例如: ```html <template> <div> <h2>{{ city }} 天气</h2> <p>当前温度: {{ temperature }}°C</p> <p>天气描述: {{ description }}</p> </div> </template> <script> import axios from 'axios'; export default { data() { return { city: '', temperature: '', description: '' }; }, async mounted() { try { const response = await axios.get('https://api.openweathermap.org/data/2.5/weather', { params: { q: '北京',appid: 'your_api_key'} }); this.city = response.data.name; this.temperature = response.data.main.temp; this.description = response.data.weather[0].description; } catch (error) { console.error(error); } } }; </script> ``` 确保替换`'your_api_key'`为实际的OpenWeatherMap API密钥。 5. **在App.vue中引入并使用Weather组件**: 将`<Weather>`标签添加到`<main>`标签内,作为App.vue的子组件。 6. **运行项目**: `cd weather-app`进入项目根目录,然后运行`npm run serve`(或`yarn serve`)启动开发服务器。
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值