设计实现天气预报查询网站,包括查询主页、城市切换、七天天气展示、天气详情页面; 要求: 1.使用Vue作为前端开发框架; 2.使用vue-router实现前端路由的定义及跳转、参数的传递等; 3.使用Axios进行数据请求管理,实现天气数据的获取 vue具体代码
时间: 2024-03-30 11:37:53 浏览: 78
以下是一个简单的天气预报查询网站的Vue代码示例,包括查询主页、城市切换、七天天气展示、天气详情页面。
首先,我们需要在`App.vue`中定义路由和导航栏:
```html
<template>
<div id="app">
<nav>
<router-link to="/">查询</router-link>
</nav>
<router-view />
</div>
</template>
<script>
export default {
name: "App",
components: {},
};
</script>
<style>
nav {
display: flex;
justify-content: center;
background-color: #2196f3;
padding: 10px;
}
nav a {
color: #fff;
margin: 0 10px;
text-decoration: none;
}
nav a.active {
font-weight: bold;
}
</style>
```
然后,在`main.js`中创建路由和Axios实例:
```javascript
import Vue from "vue";
import App from "./App.vue";
import VueRouter from "vue-router";
import axios from "axios";
Vue.use(VueRouter);
Vue.prototype.$http = axios;
const routes = [
{
path: "/",
component: Home,
},
{
path: "/weather/:city",
component: Weather,
},
];
const router = new VueRouter({
mode: "history",
routes,
});
new Vue({
router,
render: (h) => h(App),
}).$mount("#app");
```
其中,`Home`和`Weather`是两个组件,分别对应查询主页和天气详情页面。`Vue.prototype.$http = axios`语句将Axios实例添加到Vue的原型中,以便在组件中使用。
接下来,我们可以实现查询主页组件`Home.vue`:
```html
<template>
<div>
<h1>天气预报查询</h1>
<form @submit.prevent="search">
<label for="city">城市:</label>
<select v-model="city">
<option value="北京">北京</option>
<option value="上海">上海</option>
<option value="广州">广州</option>
<option value="深圳">深圳</option>
<option value="成都">成都</option>
</select>
<button type="submit">查询</button>
</form>
</div>
</template>
<script>
export default {
name: "Home",
data() {
return {
city: "北京",
};
},
methods: {
search() {
this.$router.push(`/weather/${this.city}`);
},
},
};
</script>
```
这里我们使用了一个表单来获取用户输入的城市名称,并在提交表单时将城市名称作为参数传递给`Weather`组件。
然后,我们可以实现天气详情页面组件`Weather.vue`:
```html
<template>
<div>
<h1>{{ city }}天气预报</h1>
<div>
<h2>今天</h2>
<img :src="weather.today.icon" :alt="weather.today.text" />
<p>温度:{{ weather.today.temp }}℃</p>
<p>湿度:{{ weather.today.humidity }}</p>
<p>风向:{{ weather.today.wind }}</p>
</div>
<div v-for="(day, index) in weather.forecast" :key="index">
<h2>{{ day.date }}</h2>
<img :src="day.icon" :alt="day.text" />
<p>温度:{{ day.high }}℃ ~ {{ day.low }}℃</p>
<p>风向:{{ day.wind }}</p>
</div>
</div>
</template>
<script>
export default {
name: "Weather",
data() {
return {
city: "",
weather: null,
};
},
mounted() {
this.city = this.$route.params.city;
this.getWeather();
},
watch: {
"$route.params.city": function () {
this.city = this.$route.params.city;
this.getWeather();
},
},
methods: {
async getWeather() {
const response = await this.$http.get(
`https://api.openweathermap.org/data/2.5/weather?q=${this.city}&appid=YOUR_API_KEY&units=metric&lang=zh_cn`
);
const data = response.data;
this.weather = {
today: {
temp: data.main.temp,
humidity: data.main.humidity,
wind: data.wind.speed,
icon: `https://openweathermap.org/img/w/${data.weather[0].icon}.png`,
text: data.weather[0].description,
},
forecast: [],
};
const forecastResponse = await this.$http.get(
`https://api.openweathermap.org/data/2.5/forecast?q=${this.city}&appid=YOUR_API_KEY&units=metric&lang=zh_cn`
);
const forecastData = forecastResponse.data;
for (let i = 0; i < forecastData.list.length; i += 8) {
const item = forecastData.list[i];
this.weather.forecast.push({
date: item.dt_txt.slice(0, 10),
high: item.main.temp_max,
low: item.main.temp_min,
icon: `https://openweathermap.org/img/w/${item.weather[0].icon}.png`,
text: item.weather[0].description,
wind: item.wind.speed,
});
}
},
},
};
</script>
```
在这里,我们使用了`mounted`钩子和`watch`属性来获取路由参数(城市名称)并获取天气数据。我们使用了`openweathermap`API来获取天气数据,并将其存储在`weather`对象中,然后在模板中使用`v-for`指令来循环渲染未来七天的天气信息。
最后,在`main.js`中,我们需要在Axios实例中添加API密钥:
```javascript
import Vue from "vue";
import App from "./App.vue";
import VueRouter from "vue-router";
import axios from "axios";
Vue.use(VueRouter);
Vue.prototype.$http = axios.create({
baseURL: "https://api.openweathermap.org/data/2.5",
params: {
appid: "YOUR_API_KEY",
units: "metric",
lang: "zh_cn",
},
});
const routes = [
{
path: "/",
component: Home,
},
{
path: "/weather/:city",
component: Weather,
},
];
const router = new VueRouter({
mode: "history",
routes,
});
new Vue({
router,
render: (h) => h(App),
}).$mount("#app");
```
在这里,我们使用了`axios.create`方法来创建一个新的Axios实例,并在其参数中添加了`baseURL`和`params`,以便在每个请求中自动添加API密钥和其他参数。
这是一个简单的天气预报查询网站的Vue代码示例,你可以根据实际需求进行修改和完善。
阅读全文