用Vue实现天知道案例——天气预报

用Vue实现天知道案例——天气预报

需求

  1. 按下回车或者搜索实现搜索天气
  2. 查询数据(axios接口v-model)
  3. 渲染数据(v-for)

请求路径

  1. 请求地址

http://wthrcdn.etouch.cn/weather_mini

  1. 请求方式 get
  2. 请求参数 city(查询的城市名)
  3. 响应内容:天气信息

全部代码

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>天知道</title>
</head>
<style>
    #weather{
        width: 1100px;
        height: 500px;
        margin: 150px auto;
    }
    *{
        margin: 0;
        padding: 0;
    }
    ul{
        list-style: none;
    }
    @font-face {
        font-family: 'iconfont';
        src: url('font2/iconfont.eot');
        src: url('font2/iconfont.eot?#iefix') format('embedded-opentype'),
            url('font2/iconfont.woff2') format('woff2'),
            url('font2/iconfont.woff') format('woff'),
            url('font2/iconfont.ttf') format('truetype'),
            url('font2/iconfont.svg#iconfont') format('svg');
    }
    .iconfont {
        font-family: "iconfont" !important;
        font-size: 50px;
        font-style: normal;
        -webkit-font-smoothing: antialiased;
        -moz-osx-font-smoothing: grayscale;
    }
    header{
        margin: 150px auto 50px;
        width: 200px;
        height: 50px;
    }
    .one{
        color: #00a4ff;
    }
    .two{
        color: rgb(144, 214, 38);
    }
    .three{
        color: rgb(255, 187, 0);
    }
    .search{
        width: 800px;
        height: 50px;
        border: 1px solid #00a4ff;
        margin: 0 auto;
    }
    .insearch{
        float: right;
        display: block;
        width: 150px;
        height: 50px;
        background-color: #00a4ff;
        color: #fff;
        font-size: 25px;
        text-align: center;
        line-height: 50px;
        cursor: pointer;
    }
    .decorate{
        border:none;
        width: 600px;
        margin: 0 20px;
        height: 48px;
        outline: none;
        font-size:20px;
        font-weight: 200;
    }
    .hotKey{
        margin: 10px 160px;
    }
    .hotKey a{
        text-decoration: none;
        color: #666;
        margin: 0 4px;
    }
    .hotKey a:hover{
        color:yellowgreen;
    }
    h2,
    .tem{
        color: orange;
        text-align: center;
    }
    .display ul{
        margin: 100px 0;
    }
    h2{
        font-size: 30px;
        margin: 18px;
    }
    .day{
        color: rgb(165, 165, 165);
        text-align: center;
        margin-top: 15px;
    }
    ul li{
        display: inline-block;
        width: 200px;
        height: 150px;
        border-right: 1px solid #ccc;
    }
    ul li:last-child{
        border:none;
    }
</style>
<body>
    <div id="weather">
        <header>
            <span class="iconfont one">&#xe605;</span>
            <span class="iconfont two">&#xe60a;</span>
            <span class="iconfont three">&#xe651;</span>
        </header>
        <div class="search">
            <input type="text" class="decorate" placeholder="请输入查询的天气" v-model="now" @keyup.enter="query">
            <span class="insearch" @click="query">搜   索</span>
        </div>
        <div class="hotKey"> 
            <a href="javascript:;" @click="exa('北京')">北京</a>
            <a href="javascript:;" @click="exa('上海')">上海</a>
            <a href="javascript:;" @click="exa('西安')">西安</a>
            <a href="javascript:;" @click="exa('深圳')">深圳</a>
        </div>
        <div class="display">
            <ul>
                <li v-for="item in weatherList">
                    <h2>{{item.type}}</h2>
                    <div class="tem">
                        <span>{{item.low}}</span>
                        <span>~</span>
                        <span>{{item.high}}</span>
                    </div>
                    <div class="day">{{item.date}}</div>
                </li>
            </ul>
        </div>
    </div>
    <script src="https://unpkg.com/axios/dist/axios.min.js"></script>
    <script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
    <script>
        var weather=new Vue({
            el:"#weather",
            data:{
                weatherList:[],
                now:""
            },
            methods:{
                exa:function(now){
                    this.now=now;
                    this.query();
                },
                query:function(){
                    var that=this;
                    axios.get("http://wthrcdn.etouch.cn/weather_mini?city="+this.now).then(function(response){
                        that.weatherList=response.data.data.forecast;
                    }).catch(function(err){
                        console.log(err);
                        
                    })
                }
            }
        })
    </script>
</body>
</html>

成果展示

请添加图片描述

  • 4
    点赞
  • 49
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
好的,下面我来为您介绍一下如何使用vue-router实现用户登录注册功能。 首先,我们需要安装vue-router,可以通过以下命令进行安装: ``` npm install vue-router --save ``` 然后,在main.js中引入并使用vue-router: ``` import VueRouter from 'vue-router' Vue.use(VueRouter) ``` 接下来,我们新建一个router.js文件,用于配置路由。在该文件中,我们需要定义路由路径和路由组件: ``` import Vue from 'vue' import VueRouter from 'vue-router' import Login from './components/Login.vue' import Register from './components/Register.vue' Vue.use(VueRouter) const routes = [ { path: '/login', component: Login }, { path: '/register', component: Register } ] const router = new VueRouter({ routes }) export default router ``` 在上面的代码中,我们定义了两个路由路径:/login和/register,并分别对应了Login和Register两个组件。 接下来,在App.vue中,我们需要使用<router-view>标签来显示路由组件: ``` <template> <div id="app"> <router-view></router-view> </div> </template> ``` 最后,在main.js中引入router.js,并将其挂载到Vue实例中: ``` import Vue from 'vue' import App from './App.vue' import router from './router' Vue.config.productionTip = false new Vue({ router, render: h => h(App), }).$mount('#app') ``` 至此,我们已经完成了基本的路由配置。接下来,我们可以在Login和Register组件中实现具体的登录和注册功能。 在Login组件中,我们可以使用Vue.js提供的v-model指令来绑定表单输入的数据,并在点击登录按钮时向服务器发送请求验证用户名和密码: ``` <template> <div class="login"> <h1>登录</h1> <form> <label>用户名:</label> <input type="text" v-model="username"> <br> <label>密码:</label> <input type="password" v-model="password"> <br> <button @click.prevent="login">登录</button> </form> </div> </template> <script> export default { data() { return { username: '', password: '' } }, methods: { login() { // 向服务器发送请求验证用户名和密码 } } } </script> ``` 而在Register组件中,我们可以使用v-model指令来绑定表单输入的数据,并在点击注册按钮时向服务器发送请求创建新用户: ``` <template> <div class="register"> <h1>注册</h1> <form> <label>用户名:</label> <input type="text" v-model="username"> <br> <label>密码:</label> <input type="password" v-model="password"> <br> <label>确认密码:</label> <input type="password" v-model="confirmPassword"> <br> <button @click.prevent="register">注册</button> </form> </div> </template> <script> export default { data() { return { username: '', password: '', confirmPassword: '' } }, methods: { register() { // 向服务器发送请求创建新用户 } } } </script> ``` 到这里,我们已经完成了一个简单的用户登录注册功能,并使用vue-router实现了路由跳转。当用户访问/login时,会显示Login组件;当用户访问/register时,会显示Register组件。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值