使用Vue制作天气预报界面

要求:

使用Axios请求数据,在页面中显示近七天的天气情况

效果图:

 进人http://ww.tianqiapi.com,注册开发账号,并获得appid与appsecret

完整代码如下:


<template>
  <div id="app">
    <nav>{{ update_time }} {{ week }}</nav>
    <div class="hello">
      <!-- 城市信息 -->
      <h2>{{ city }}</h2>
      <h4>{{ message }}</h4>
      <h2>{{ wea }}</h2>
      <h1>{{ tem }}℃</h1>
      <div class="detail">
        <span>风力{{ win_speed }}&nbsp;|&nbsp;{{ win }}&nbsp;|&nbsp;空气质量:{{ air_level }}</span>
      </div>
      <!-- 天气列表 -->
      <ul class="list">
        <li v-for="item in obj">
          <div>
            <h3>{{ item.data }}</h3>
            <h3>{{ item.week }}</h3>
            <img :src="get(item.wea_img)" alt="" />
            <h3>{{ item.wea }}</h3>
            <h3>{{ item.tem1 }}℃-{{item.tem2}}℃</h3>
          </div>
        </li>
      </ul>
    </div>
  </div>
</template>

<script >
import axios from "axios"; //引入axios 
export default{
  name: "HelloWorld",
  data() {
    return {
      city: "",
      obj: [],
      date: "",
      week: "",
      wea:"",
      message: "",
    };
  },
  methods: {
    get(sky) {
      //图片格式
      return "https://xintai.xianguomall.com/skin/pitaya/" + sky + ".png";
    },
  },
  created() {
    this.get(); 
    var that = this;
    axios
      .get(
        "https://v0.yiketianqi.com/api?unescape=1&version=v91&appid=12345678&appsecret=abcdefgh&city=重庆"
      )//这里的id和appsecret用12345678和abcdefgh代替,替换成自己的即可
      .then(function (response) {
        //处理数据
        that.city = response.data.city;//城市
        that.obj = response.data.data;
        that.update_time = response.data.update_time;//时间
        that.week = response.data.data[0].week;
        that.wea = response.data.data[0].wea;//天气状况
        that.tem = response.data.data[0].tem;//温度
        that.win_speed = response.data.data[0].win_speed;//风力
        that.win = response.data.data[0].win;//风向
        that.air_level = response.data.data[0].air_level;//空气质量
        that.message = response.data.data[0].air_tips;
      })
      .catch(function (error) {
        console.log("请求失败");
      });
  },
}
</script>

<style scoped> 
h2{
  font-size:20px ;
} 
h2,
h4 {
    text-align: center;
}
nav{
  display: flex;
   padding: 10px;
}
.list{
  padding-top:20px ;
  max-width: 1480px;
  margin: 0 auto;
}
li {
    float: left;
    list-style-type: none;
    width: 200px;
    text-align: center;
    margin-top:20px ;
}

</style>

  • 4
    点赞
  • 26
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
首先需要安装 Vue CLI,可以在命令行工具中输入以下命令安装: ``` npm install -g @vue/cli ``` 然后创建一个新的 Vue 项目: ``` vue create my-project ``` 接下来,我们可以在 `src` 文件夹中创建 `components` 文件夹,并在其中创建 `Login.vue` 和 `Register.vue` 两个组件。 在 `Login.vue` 和 `Register.vue` 中,我们可以使用 Vue 的模板语法和样式来设计登录和注册界面,例如: ```html <template> <div class="login"> <h2>{{ title }}</h2> <form> <div class="form-group"> <label>Username:</label> <input type="text" v-model="username" /> </div> <div class="form-group"> <label>Password:</label> <input type="password" v-model="password" /> </div> <button @click.prevent="submit">{{ button }}</button> </form> </div> </template> <script> export default { data() { return { title: "Login", username: "", password: "", button: "Submit" }; }, methods: { submit() { // 处理表单提交事件 } } }; </script> <style scoped> .login { width: 300px; margin: 0 auto; padding: 20px; border: 1px solid #ccc; border-radius: 5px; } .form-group { margin-bottom: 10px; } label { display: block; margin-bottom: 5px; } input[type="text"], input[type="password"] { width: 100%; padding: 5px; border: 1px solid #ccc; border-radius: 3px; box-sizing: border-box; } button { display: block; margin-top: 10px; padding: 5px; border: none; border-radius: 3px; background-color: #007bff; color: #fff; cursor: pointer; } </style> ``` 在 `Login.vue` 和 `Register.vue` 中,我们可以定义数据和方法来实现登录和注册功能。 最后,在 `App.vue` 中,我们可以使用 `router-link` 和 `router-view` 来实现页面导航和组件切换,例如: ```html <template> <div id="app"> <nav> <router-link to="/login">Login</router-link> <router-link to="/register">Register</router-link> </nav> <router-view></router-view> </div> </template> <script> export default { name: "App" }; </script> <style> nav { display: flex; justify-content: center; margin-bottom: 20px; } nav a { display: inline-block; padding: 10px; margin-right: 10px; text-decoration: none; color: #007bff; border: 1px solid #007bff; border-radius: 5px; } nav a.active { background-color: #007bff; color: #fff; } nav a:hover { background-color: #007bff; color: #fff; } </style> ``` 最后,需要在 `main.js` 中引入 `vue-router` 并配置路由,例如: ```javascript import Vue from "vue"; import App from "./App.vue"; import Login from "./components/Login.vue"; import Register from "./components/Register.vue"; import VueRouter from "vue-router"; Vue.use(VueRouter); const routes = [ { path: "/login", component: Login }, { path: "/register", component: Register } ]; const router = new VueRouter({ routes }); Vue.config.productionTip = false; new Vue({ router, render: h => h(App) }).$mount("#app"); ``` 这样,我们就完成了一个简单的登录注册界面

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值