主頁 > 移動端開發 > 拿來吧你!不想學Vue?我不答應!入門Vue必備基礎-看這里!

拿來吧你!不想學Vue?我不答應!入門Vue必備基礎-看這里!

2021-07-21 14:11:18 移動端開發

拿來吧你——Vue官網:https://cn.vuejs.org/

目錄

1. 入門必知

1.1 了解Vue

1.2 基礎知識

2. 基礎必學

2.1 v-text指令

2.2 v-html指令

2.3 v-on指令

2.4 v-show指令

2.5 v-if指令

2.6 v-bind指令

2.7 v-for指令

2.8 v-model指令

3.案例參考

3.1 計數器

3.2 記事本

3.3 輪播圖

3.4 天氣預報

3.5 看笑話案例


不想學Vue?我不答應!入門Vue必備基礎-看這里!

手把手教你快速入門,僅需學會幾個知識點+小案例,分分鐘入門不是大問題!

所有案例已打包,點擊下載:https://download.csdn.net/download/qq_44140450/20360901

1. 入門必知

1.1 了解Vue

是一套用于構建用戶界面的漸進式框架,與其它大型框架不同的是,Vue 被設計為可以自底向上逐層應用,Vue 的核心庫只關注視圖層,不僅易于上手,還便于與第三方庫或既有專案整合,另一方面,當與現代化的工具鏈以及各種支持類別庫結合使用時,Vue 也完全能夠為復雜的單頁應用提供驅動,

1.2 基礎知識

你需要學會:

  1. 引入vue:
    
    
    <!-- 開發環境版本,包含了有幫助的命令列警告 -->
    <script src="https://cdn.jsdelivr.net/npm/vue@2/dist/vue.js"></script>
    
    <!--或者:-->
    
    <!-- 生產環境版本,優化了尺寸和速度 -->
    <script src="https://cdn.jsdelivr.net/npm/vue@2"></script>
  2. 掌握幾個必要的指令:


  1. v-text指令;
  2. v-html指令;
  3. v-on指令
  4. v-show指令
  5. v-if指令
  6. v-bind指令
  7. v-for指令
  8. v-model指令

2. 基礎必學

2.1 v-text指令

  1. 作用:設定標簽的內容
  2. 默認寫法:
    <p v-text = " msg " ></p>
  3. 差值寫法:

    <p> {{ msg }} </p>
  4. 案例

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <title>v-text指令案例</title>
        <style>
            *{
                margin: 0;
                padding: 0;
            }
            #app{
                position: absolute;
                left: 50%;
                top: 20%;
                transform: translateY(-20%) translateX(-50%);
            }
            #app *{
                margin: 10px;
            }
            #app ul li{
                list-style-type: none;
                border-bottom: 1px solid #666;
                text-align: center;
            }
        </style>
    </head>
    <body>
        <div id="app">
            <h1 v-text = "msg"></h1>
            <ul>
                <li v-text = 'myMsg[0]'></li>
                <li>{{myMsg[1]}}</li>
                <li>{{myMsg[2] + "人"}}</li>
            </ul>
        </div>
        <script src="https://cdn.jsdelivr.net/npm/vue@2/dist/vue.js"></script>
        <script>
            var vue = new Vue({
                el:"#app",
                data:{
                    msg:"hello world!",
                    myMsg:["海子哥","2749984520@qq.com","吉安永新"]
                }
            })
        </script>
    </body>
    </html>
  5. 效果

2.2 v-html指令

  1. 作用:設定元素innerHtml
  2. v-html中有html結構會被自動決議為標簽,
  3. 與v-text區別
    1. v-text:無論內容是什么,只會決議為文本,
    2. v-html:能自動決議html結構,
  4. 案例
    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <title>v-html指令</title>
        <style>
            *{
                margin: 0;
                padding: 0;
            }
            #app{
                position: absolute;
                left: 50%;
                top: 20%;
                transform: translateY(-20%) translateX(-50%);
            }
            #app *{
                font-size: 14px;
                color: #666;
                margin: 10px;
                text-align: center;
            }
        
        </style>
    </head>
    <body>
        <div id= 'app' >
            <p v-text = "con"></p>
            <p v-html = "content"></p>
            <p v-text = "content"></p>
        </div>
        <script src="https://cdn.jsdelivr.net/npm/vue@2/dist/vue.js"></script>
        <script>
            var vue = new Vue({
                el:"#app",
                data:{
                    content:"<a href='https://blog.csdn.net/qq_44140450'>海子哥的主頁</a>",
                    con:"海子哥",
                    myMsg:["海子哥","188207245","吉安永新"]
                }
            })
        </script>
    </body>
    </html>
  5. 運行效果

2.3 v-on指令

  1. 作用:為元素系結事件,
  2. 寫法:
    1. 標準寫法
      <!--以click事件為例-->
      <button v-on:click="方法名">按鈕</button>
    2. 簡寫方式

      <!--以click事件為例-->
      <input type="button" @click="方法名" value="按鈕">
  3. 案例

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <title>v-on指令</title>
        <style>
            *{
                margin: 0;
                padding: 0;
            }
            #app{
                position: absolute;
                left: 50%;
                top: 20%;
                transform: translateY(-20%) translateX(-50%);
            }
            #app *{
                font-size: 14px;
                color: #666;
                margin: 10px;
                text-align: center;
                
            }
        </style>
    </head>
    <body>
        <div id="app">
            <input type="button" v-on:click = "click_on" value="單擊滑鼠事件">
            <input type="button" v-on:mouseleave = "mouse_leave" value="滑鼠離開事件">
            <input type="button" v-on:mouseenter = "mouse_enter" value="滑鼠進入事件">
            <input type="button" @mouseenter = "mouse_move" value="滑鼠進入事件">
        </div>
        <script src="https://cdn.jsdelivr.net/npm/vue@2/dist/vue.js"></script>
        <script>
            var vue = new Vue({
                el:"#app",
                methods:{
                    click_on(){
                        alert("單擊了按鈕");
                    },
                    mouse_leave:function(){
                        alert("滑鼠離開")
                    },
                    mouse_enter(){
                        alert("滑鼠進入!")
                    },
                    mouse_move:function(){
                        alert("滑鼠移動了!")
                    }
                }
                
            })
        </script>
    </body>
    </html>
  4. 運行效果

2.4 v-show指令

  1. 本質是切換元素的display屬性
  2. 作用:根據bool值來定義dispaly屬性,
  3. v-show的內容最終決議為一個bool值
  4. 案例
    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <title>v-show指令</title>
        <style>
            *{
                margin: 0;
                padding: 0;
            }
            #app{
                position: absolute;
                width: 960px;
                height: 540px;
                left: 50%;
                top: 0;
                transform: translateX(-50%);
            }
            #app *{
                display: inline;
                font-size: 14px;
                color: #666;
                margin: 10px;
                text-align: center;
            }
            #app button{
                top: 0;
                outline: 0;
                border: 1px solid pink;
                background-color: pink;
                border-radius: 5px;
            }
            #app button:hover{
                transition: 0.3s;
                box-shadow: 0 0 12px rgb(243, 133, 151);
            }
            #app img{
                width: 960px;
                height: 540px;
            }
        </style>
    </head>
    <body>
        <div id="app">
            <button @click="on">點擊查看物件</button>
            <br>
            <img src="./img/1.jpg" v-show="isShow">
        </div>
        <script src="https://cdn.jsdelivr.net/npm/vue@2/dist/vue.js"></script>
        <script>
            var vue = new Vue({
                el:"#app",
                data:{
                    isShow:false,
                },
                methods:{
                    on:function(){
                        this.isShow = !this.isShow;
                    }
                }
            })
        </script>
    </body>
    </html>
  5. 運行結果

2.5 v-if指令

  1. 本質:修改dom元素,
  2. 作用:根據表達值(bool)切換元素狀態,
  3. 運算式值為ture時,元素存在于Dom樹中;否則,元素從Dom樹中移除,
  4. 頻發切換狀態下使用v-if,系統資源消耗大,建議這種狀態下使用v-show.
  5. 案例
    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <title>v-if指令</title>
        <!-- border -->
        <style>
            *{
                margin: 0;
                padding: 0;
            }
            #app{
                position: absolute;
                width: 960px;
                height: 540px;
                left: 50%;
                top: 0;
                transform: translateX(-50%);
            }
            #app *{
                display: inline;
                font-size: 14px;
                color: #666;
                margin: 10px;
                text-align: center;
            }
            #app button{
                top: 0;
                outline: 0;
                border: 1px solid pink;
                background-color: pink;
                border-radius: 5px;
            }
            #app button:hover{
                transition: 0.3s;
                box-shadow: 0 0 12px rgb(243, 133, 151);
            }
            #app img{
                width: 960px;
                height: 540px;
            }
        </style>
    </head>
    <body>
        <div id="app">
            <button @click="on">點擊一下查看物件</button>
            <img  src="./img/3.jpg" v-if="isShow">
        </div>
        <script src="https://cdn.jsdelivr.net/npm/vue@2/dist/vue.js"></script>
        <script>
            var vue = new Vue({
                el:"#app",
                data:{
                    isShow:false,
                },
                methods:{
                    on:function(){
                        this.isShow = !this.isShow;
                    }
                }
            })
        </script>
    </body>
    </html>
  6. 運行效果

2.6 v-bind指令

  1. 作用:設定元素屬性,為元素系結屬性,
  2. 格式:
    1. 標準寫法
      v-bind:屬性名 = 運算式
    2. 簡寫方式

      :屬性名
    3. 動態增刪屬性的兩種方式

      <!--通過類名進行系結(推薦使用)-->
      <p v-bind:屬性名="{'類名':bool值}"></p>
      
      
      
      <!--使用三目運算子進行系結-->
      <p :屬性名="bool值?'類名':''"></p>
  3. 案例

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <title>v-bind指令</title>
        <style>
           *{
                margin: 0;
                padding: 0;
            }
            #app{
                position: absolute;
                width: 960px;
                height: 540px;
                left: 50%;
                top: 0;
                transform: translateX(-50%);
            }
            #app *{
                display: inline;
                font-size: 14px;
                color: #666;
                margin: 10px;
                text-align: center;
            }
            #app button{
                top: 0;
                outline: 0;
                border: 1px solid pink;
                background-color: pink;
                border-radius: 5px;
            }
            #app button:hover{
                transition: 0.3s;
                box-shadow: 0 0 12px rgb(243, 133, 151);
            }
            #app img{
                width: 960px;
                height: 540px;
            }
            .active{
                transition: 0.5s;
                box-shadow: 2px 10px 12px rgb(238, 60, 90);
                border-radius: 30px;
                transform: translateY(-10px) translateZ(-10px);
            }
        </style>
    </head>
    <body>
        <div id="app">
            <button @click="on">點擊一下</button>
            <img  v-bind:src="imgSrc"
             :title="title"
             :class="isActive?'active':''">
        </div>
        <script src="https://cdn.jsdelivr.net/npm/vue@2/dist/vue.js"></script>
        <script>
            var vue = new Vue({
                el:"#app",
                data:{
                    imgSrc:"./img/7.jpg",
                    isShow:false,
                    title:"188207245-肖家海",
                    isActive:false,
                },
                methods:{
                    on:function(){
                        this.isActive = !this.isActive;
                    }
                }
            })
        </script>
    </body>
    </html>
  4. 運行效果

2.7 v-for指令

  1. 回應式生成串列結構,
  2. 作用:根據資料生成串列結構,陣列資料經常和v-for結合使用,
  3. 原始碼案例
    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <title>v-for指令</title>
        <style>
             *{
                margin: 0;
                padding: 0;
            }
            #app{
                position: absolute;
                left: 50%;
                top: 20%;
                transform: translateX(-50%) translateY(-20%);
            }
            #app *{
                margin: 10px;
            }
            #app input{
                top: 0;
                outline: 0;
                border: 1px solid darkslateblue;
                background-color: #fff;
                border-radius: 5px;
            }
            #app input:hover{
                transition: 0.3s;
                color: #fff;
                background-color: darkslateblue;
                box-shadow: 0 0 12px darkslateblue;
            }
            #app li{
                list-style-type: none;
                text-align: center;
            }
        </style>
    </head>
    <body>
           
        <div id="app">
            <ul>
                <li v-for="item in arr">
                    {{ item }}
                </li>
            </ul>
    
            <input type="button" value="添加一個阿姨聯系方式" @click="add">
            <input type="button" value="移除一個阿姨聯系方式" @click="remove">
            <ul>
                <li v-for="(obj,index) in ayi">
                    {{index+1}}、{{ obj.name }}的電話號碼是:{{ obj.phone }}
                </li>
            </ul>
        </div>
    
    <script src="https://cdn.jsdelivr.net/npm/vue@2/dist/vue.js"></script>
    <script>
        var vue = new Vue({
            el:"#app",
            data:{
                arr:["上海王阿姨",
                "北京李阿姨",
                "重慶劉阿姨",
                "南京胡阿姨"],
                ayi:[
                    {name:"上海王阿姨",phone:'1558596521'},
                    {name:"北京李阿姨",phone:'1365520452'},
                    {name:"重慶劉阿姨",phone:'1875554214'},
                    {name:"廣州趙阿姨",phone:'1985565421'},
                    {name:"南京胡阿姨",phone:'1865574258'}
                ],
            },
            methods:{
                add:function(){
                    this.ayi.push({name:"江蘇蘇阿姨",phone:'1998562478'});
                },
                remove:function(){
                    this.ayi.shift();
                }
            }
        });
    </script> 
    </body>
    </html>
  4. 運行效果

2.8 v-model指令

  1. v-model是一種雙向資料系結,
  2. 作用:設定、獲取表單元素的值,(同時解決了單標簽元素無法使用v-text指令的缺點),
  3. 系結的資料會和表單元素值進行雙向關聯,
  4. 代碼案例
    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <title>v-model指令</title>
        <style>
            *{
                margin: 0;
                padding: 0;
            }
            #app{
                position: absolute;
                left: 50%;
                top: 20%;
                transform: translateX(-50%) translateY(-20%);
                font-size:0 ;
            }
            #app *{
                height: 30px;
                border: 1px solid darkslateblue;
                outline: 0;
                font-size: 0;
                font-size:18px ;
            }
            #app input[type="text"]{
                padding-left: 5px;
                width: 200px;
                border-radius: 10px 0px 0 10px;
            }
            #app input[type="button"]{
                width: 60px;
                height: 32px;
                border-radius: 0 10px 10px 0;
                vertical-align: top;
                background-color: darkslateblue;
                color: #fff;
            }
            #app input[type="button"]:hover{
                transition: 0.3s;
                box-shadow: 0 0 12px darkslateblue;
            }
        </style>
    </head>
    <body>
        <div id="app">
            <input type="text" @keyup.enter = "enter" v-model="val"><!--
           --><input type="button" value="按他" @click="enter">
        </div>
    
    <script src="https://cdn.jsdelivr.net/npm/vue@2/dist/vue.js"></script>
    <script>
        var vue = new Vue({
            el:"#app",
            data:{
                val:"",
            },
            methods:{
                enter:function(){
                    if(this.val.trim() != "")
                        alert("value:"+this.val);
                }
            }
        });
    </script> 
    </body>
    </html>
  5. 運行效果

3.案例參考

3.1 計數器

  1. 參考原始碼
    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <title>counter</title>
        <style>
            *{
                margin: 0;
                padding: 0;
            }
            #context{
                position:absolute;
                /* background-color: aquamarine; */
                display: inline-block;
                left: 50%;
                top: 50%;
    
                transform: translateX(-50%) translateY(-50%);
            }
            #app{
                border-radius: 5px;
                box-shadow: 0 0 5px #666;
            }
            #app button{
                width: 60px;
                height: 40px;
                border: 0;
                outline: none;
                /* border-radius: 5px; */
                background-color: #ccc;
                font-size: 28px;
            }
            #app button:hover{
                transition:  0.5s;
                background-color: darkslateblue;
                color: #fff;
                /* box-shadow: 0 0 5px darkslateblue; */
            }
            #app span{
                margin: 20px;
                font-size: 18px;
            }
        </style>
    </head>
    <body>
        <div id="context">
            <div id="app">
                <button @click="sub" style="border-radius:5px 0 0 5px;">-</button>
                <span >{{ num }}</span>
                <button @click="add" style="border-radius:0 5px 5px 0;">+</button>
            </div>
        </div>
        <script src="https://cdn.jsdelivr.net/npm/vue@2/dist/vue.js"></script>
        <script>
            var vue = new Vue({
                el:"#app",
                data:{
                    num:1,
                },
                methods:{
                    add:function(){
                        this.num = this.num >= 10? this.num:this.num+1;
                    },
                    sub:function(){
                        this.num = this.num <=1 ? this.num:this.num-1;
                    }
                }
            })
        </script>
    </body>
    </html>
  2. 運行效果

3.2 記事本

  1. 參考原始碼
    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <title>記事本</title>
        <style>
            *{
                margin: 0;
                padding: 0;
                font-family: "YouYuan";
            }
            #notepad{
                position: absolute;
                width: 50%;
                height: 50%;
                left:50%;
                top:50%;
                transform: translateX(-50%) translateY(-50%);
                box-shadow: 0 0 8px #666;
                border-radius: 10px;
            }
            #notepad div{
                width: 100%;
                height: 60%;
                overflow: auto;
            }
            #notepad h1{
                width: 100%;
                padding-top: 10px;
                margin-bottom: 10px;
                text-align: center;
            }
            #notepad ul{
                width: 90%;
                margin-left: 5%;
            }
            #notepad ul li{
                list-style-type: none;
                padding-left: 10px;
                /* border-bottom: 1px solid #666; */
                font-size: 18px;
                padding-bottom: 5px;
                padding-top: 5px;
            }
            #notepad ul li:hover input{
                visibility:visible;
            }
            #notepad input[type="text"]{
                width: 60%;
                margin-left: 10%;
                height: 25px;
                margin-bottom: 5px;
                margin-top: 5px;
                margin-right: 0;
                border:0;
                font-size: 18px;
                outline: none;
                border-radius: 5px 0 0 5px;
                box-shadow: 0 0 5px darkslateblue;
            }
            #notepad #sure{
                width: 50px;
                height: 25px;
                margin-bottom: 5px;
                margin-top: 5px;
                margin-left: -6px;
                border:0;
                outline: none;
                border-radius: 5px;
                background-color: darkslateblue;
                color: #fff;
                transform: translateY(-1px);
                 border-radius:0 5px 5px 0;
            }
            #notepad #sure:hover{
                transition: 0.3s;
                box-shadow: 0 0 5px darkslateblue;
            }
    
            #notepad #left-p{
                float:left;
                margin: 15px auto 5px 5px;
                border-bottom: 1px solid darkslateblue;
            }
            #notepad #right-but{
                float:right;
                border-radius: 5px;
                margin: 15px 5px 5px auto;
                border: 1px solid darkslateblue;
            }
            #notepad #right-but:hover{
                transition: 0.3s;
                background-color: darkslateblue;
                color: #fff;
                box-shadow: 0 0 5px red;
            }
            #notepad .remove-but{
                visibility: hidden;
                width: 20px;
                height: 20px;
                color: red;
                border-radius: 20px;
                float: right;
                border: 0;
                text-align: center;
                margin-right: 5px;
                font-size: 18px;
            }
        </style>
    </head>
    <body>
        <div id="notepad">
            <h1>記事本</h1>
            <input type="text" v-model="addVal" @keyup.enter="add">
            <input type="button" id="sure" value="確認" @click="add">
            <div>
                <ul >
                    <li v-for="(item,index) in val" >
                        {{index+1}}.{{ item }}
                        <input class="remove-but" type="button" value="-" @click="clean(index)" >
                    </li>
                </ul>
             </div>
    
            <p id="left-p" v-if = "val.length != 0">已有事務{{val.length}}條</p>
            <input id="right-but" v-show = "val.length != 0" type="button" value="全部清除" @click = "cleanAll">
        </div>
    
        <script src="https://cdn.jsdelivr.net/npm/vue@2/dist/vue.js"></script>
    <script>
        var vue = new Vue({
            el:"#notepad",
            data:{
                val:[],
                addVal:"",
                isShowCleanBut:false,
            },
            methods:{
                add:function(){
                    if(this.addVal.trim() != ""){
                        this.val.push(this.addVal.trim());
                        this.addVal="";
                    }
                },
                clean:function(index){
                    this.val.splice(index,1);
                },
                cleanAll:function(){
                    this.val=[];
                }
            }
        });
    </script> 
    </body>
    </html>
  2. 運行效果

3.3 輪播圖

  1. 參考原始碼
    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <title>輪播圖</title>
        <style>
            *{
                margin: 0;
                padding: 0;
            }
            #app{
                width: 100%;
                height: 100%;
            }
            #context{
                position: absolute;
                left:50%;
                top: 50%;
                transform: translateX(-50%) translateY(-50%);
            }
            img{
                width: 768px;
                height: 432px;
            }
            #app img{
                transition: all 0.3s;
                display: inline-block;
            }
            #app button{
                position: absolute;
                top: 50%;
                transform: translateY(-50%);
                margin: 2px;
                width: 30px;
                height: 60px;
                border: 0;
                outline: none;
                color: #fff;
                font-size: 18px;
                border-radius: 2px;
                background-color: rgba(58, 58, 58, 0.548);
            }
            #app button:hover{
                transition: 0.3s;
                box-shadow: 0 0 5px #666;
            }
        </style>
    </head>
    <body>
        <div id="app">
            <div id = "context">
                <button style="float: left;" @click="sub" >?</button>
                <img  v-bind:src="imgSrc[index]">
                <button style="float: right;transform:translateX(-34px) translateY(-50%);" @click="add">?</button>
            </div>
        </div>
        <script src="https://cdn.jsdelivr.net/npm/vue@2/dist/vue.js"></script>
        <script>
            var vue = new Vue({
                el:"#app",
                data:{
                    imgSrc:["./img/7.jpg",
                            "./img/1.jpg",
                            "./img/2.jpg",
                            "./img/3.jpg",
                            "./img/4.jpg",
                            "./img/5.jpg",
                            "./img/6.jpg"],
                    index: 0,
                },
                methods:{
                    add:function(){
                        this.index =this.index < this.imgSrc.length-1? this.index + 1 :0;
                    },
                    sub:function(){
                        this.index = this.index > 0? this.index - 1:this.imgSrc.length-1;
                    }
                }
            });
        </script>
    </body>
    </html>

  2. 運行效果

3.4 天氣預報

  1. 參考原始碼
    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <title>查詢天氣案例</title>
        <style>
            *{
                margin: 0;
                padding: 0;
            }
            #app{
                position: absolute;
                width: 60%;
                height: 50%;
                top: 10%;
                left: 50%;
                transform: translateX(-50%) translateY(-10%);
            }
            /* #app .form_group{
                margin-bottom: 5px;
            } */
            #app .form_group input,
            #app .form_group button {
                
                margin: 0;
                padding: 0;
                display: inline-block;
                outline:none; 
                
                
            }
            #app .form_group input[type='text']{
                height: 31px;
                font-size: 15px;
                width: 80%;
                border: 2px solid darkslateblue;
            }
            #app .form_group button{
                background-color: darkslateblue;
                color: #fff;
                width: 15%;
                height: 35px;
                border: 0;
                vertical-align: top;
                transform: translateX(-1px);
            }
    
            #app .hot-city{
                margin: 5px;
            }
            #app .hot-city a{
                font-size: 14px;
                color: #666;
                text-decoration: none;
            }
    
            #app .weather-list li{
                display: inline-block;
                /* background-color: darkslategray; */
                padding: 15px;
                border-right: 1px solid #666;
            }
           
            #app ul :nth-last-child(1){
                border: 0;
            }
            #app .info_type,
            #app .info_temp,
            #app .info-date{
                text-align: center;
                padding-bottom: 15px;
            }
            #app .info_type{
                color: darkslateblue;
                font-size: 16px;
               
            }
            #app .info_temp{
                color: darkslateblue;
                font-size: 8px;
            }
            #app .info-date{
                color: #666;
            }
        </style>
    </head>
    <body>
        <div id="app">
            <div>
                <div class="form_group">
                    <input type="text" @keyup.enter="down" v-model="city"><!----><button @click="down">查詢</button>
                </div>
                <div class="hot-city">
                    <a href="javascript:;" @click="hot_city('北京')">北京</a>
                    <a href="javascript:;" @click="hot_city('上海')">上海</a>
                    <a href="javascript:;" @click="hot_city('廣州')">廣州</a>
                    <a href="javascript:;" @click="hot_city('深圳')">深圳</a>
                    <a href="javascript:;" @click="hot_city('南昌')">南昌</a>
                </div>
            </div>
            <ul class="weather-list" >
                <li v-for="item in weatherList">
                    <div class="info_type">
                        <span class="iconfont">
                           {{item.type}} 
                        </span>
                    </div>
                    <div class="info_temp">
                        <b>
                            {{item.low}}
                        </b>
                        ~
                        <b>
                            {{item.high}}
                        </b>
                    </div>
                    <div class="info-date">
                        <span>
                            {{item.date}}
                        </span>
                    </div>
                </li>
            </ul>
         </div>
    
     <script src="https://unpkg.com/axios/dist/axios.min.js"></script>
     <script src="https://cdn.jsdelivr.net/npm/vue@2/dist/vue.js"></script>
     <script>
         var vue = new Vue({
             el:"#app",
             data:{
                 city:"",
                 weatherList:[],
             },
             methods:{
                 down:function(){
                     var that = this;
                     axios.get("http://wthrcdn.etouch.cn/weather_mini?city="
                     +this.city).then(
                         function(response){
                            that.weatherList = response.data.data.forecast;
                        },function(err){alert("連接失敗!")}
                    )
                 },
                 hot_city(city){
                    this.city=city;
                    this.down();
                 }
             }
         });
     </script> 
    </body>
    </html>

  2. 運行效果

3.5 看笑話案例

  1. 參考原始碼
    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <title>看笑話</title>
        <style>
            *{
                margin: 0;
                padding: 0;
            }
            #app{
                position: absolute;
                left: 50%;
                top: 20%;
                transform: translateX(-50%) translateY(-20%);
                font-size:0 ;
                margin: 5px;
            }
            #app *{
                margin: 5px;
                outline: none;
                border: 1px solid darkslateblue;
            }
            #app input{
                position: relative;
                width: 100px;
                height: 26px;
                left: 50%;
                transform: translateX(-50%);
                border-radius: 13px;
            }
            #app input:hover{
                transition: 0.3s;
                color: #fff;
                background-color: darkslateblue;
                box-shadow:  0 0 12px darkslateblue;
            }
            #app textarea{
                width: 600px;
                height: 300px;
            }
        </style>
    </head>
    <body>
        <div id="app">
            <input type="button" value="來一個笑話" @click="down">
            <textarea >{{ text }}</textarea>
         </div>
    
     <script src="https://unpkg.com/axios/dist/axios.min.js"></script>
     <script src="https://cdn.jsdelivr.net/npm/vue@2/dist/vue.js"></script>
     <script>
         var vue = new Vue({
             el:"#app",
             data:{
                 text:"",
             },
             methods:{
                 down:function(){
                     var that = this;
                     axios.get("https://autumnfish.cn/api/joke").then(
                         function(response){
                             var str= response.data;
                            that.text = response.data;
                        },function (err){alter("連接失敗!")}
                    )
                 },
             }
         });
     </script> 
    </body>
    </html>
  2. 運行效果

轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/289337.html

標籤:其他

上一篇:Android:怎么看待大廠面試門檻越來越高,“面試造火箭,作業擰螺絲釘”現象越來越普遍?

下一篇:Kotlin基礎語法

標籤雲
其他(157675) Python(38076) JavaScript(25376) Java(17977) C(15215) 區塊鏈(8255) C#(7972) AI(7469) 爪哇(7425) MySQL(7132) html(6777) 基礎類(6313) sql(6102) 熊猫(6058) PHP(5869) 数组(5741) R(5409) Linux(5327) 反应(5209) 腳本語言(PerlPython)(5129) 非技術區(4971) Android(4554) 数据框(4311) css(4259) 节点.js(4032) C語言(3288) json(3245) 列表(3129) 扑(3119) C++語言(3117) 安卓(2998) 打字稿(2995) VBA(2789) Java相關(2746) 疑難問題(2699) 细绳(2522) 單片機工控(2479) iOS(2429) ASP.NET(2402) MongoDB(2323) 麻木的(2285) 正则表达式(2254) 字典(2211) 循环(2198) 迅速(2185) 擅长(2169) 镖(2155) 功能(1967) .NET技术(1958) Web開發(1951) python-3.x(1918) HtmlCss(1915) 弹簧靴(1913) C++(1909) xml(1889) PostgreSQL(1872) .NETCore(1853) 谷歌表格(1846) Unity3D(1843) for循环(1842)

熱門瀏覽
  • 【從零開始擼一個App】Dagger2

    Dagger2是一個IOC框架,一般用于Android平臺,第一次接觸的朋友,一定會被搞得暈頭轉向。它延續了Java平臺Spring框架代碼碎片化,注解滿天飛的傳統。嘗試將各處代碼片段串聯起來,理清思緒,真不是件容易的事。更不用說還有各版本細微的差別。 與Spring不同的是,Spring是通過反射 ......

    uj5u.com 2020-09-10 06:57:59 more
  • Flutter Weekly Issue 66

    新聞 Flutter 季度調研結果分享 教程 Flutter+FaaS一體化任務編排的思考與設計 詳解Dart中如何通過注解生成代碼 GitHub 用對了嗎?Flutter 團隊分享如何管理大型開源專案 插件 flutter-bubble-tab-indicator A Flutter librar ......

    uj5u.com 2020-09-10 06:58:52 more
  • Proguard 常用規則

    介紹 Proguard 入口,如何查看輸出,如何使用 keep 設定入口以及使用實體,如何配置壓縮,混淆,校驗等規則。

    ......

    uj5u.com 2020-09-10 06:59:00 more
  • Android 開發技術周報 Issue#292

    新聞 Android即將獲得類AirDrop功能:可向附近設備快速分享檔案 谷歌為安卓檔案管理應用引入可安全隱藏資料的Safe Folder功能 Android TV新主界面將顯示電影、電視節目和應用推薦內容 泄露的Android檔案暗示了傳說中的谷歌Pixel 5a與折疊屏新機 谷歌發布Andro ......

    uj5u.com 2020-09-10 07:00:37 more
  • AutoFitTextureView Error inflating class

    報錯: Binary XML file line #0: Binary XML file line #0: Error inflating class xxx.AutoFitTextureView 解決: <com.example.testy2.AutoFitTextureView android: ......

    uj5u.com 2020-09-10 07:00:41 more
  • 根據Uri,Cursor沒有獲取到對應的屬性

    Android: 背景:呼叫攝像頭,拍攝視頻,指定保存的地址,但是回傳的Cursor檔案,只有名稱和大小的屬性,沒有其他諸如時長,連ID屬性都沒有 使用 cursor.getInt(cursor.getColumnIndexOrThrow(MediaStore.Video.Media.DURATIO ......

    uj5u.com 2020-09-10 07:00:44 more
  • Android連載29-持久化技術

    一、持久化技術 我們平時所使用的APP產生的資料,在記憶體中都是瞬時的,會隨著斷電、關機等丟失資料,因此android系統采用了持久化技術,用于存盤這些“瞬時”資料 持久化技術包括:檔案存盤、SharedPreference存盤以及資料庫存盤,還有更復雜的SD卡記憶體儲。 二、檔案存盤 最基本存盤方式, ......

    uj5u.com 2020-09-10 07:00:47 more
  • Android Camera2Video整合到自己專案里

    背景: Android專案里呼叫攝像頭拍攝視頻,原本使用的 MediaStore.ACTION_VIDEO_CAPTURE, 后來因專案需要,改成了camera2 1.Camera2Video 官方demo有點問題,下載后,不能直接整合到專案 問題1.多次拍攝視頻崩潰 問題2.雙擊record按鈕, ......

    uj5u.com 2020-09-10 07:00:50 more
  • Android 開發技術周報 Issue#293

    新聞 谷歌為Android TV開發者提供多種新功能 Android 11將自動填表功能整合到鍵盤輸入建議中 谷歌宣布Android Auto即將支持更多的導航和數字停車應用 谷歌Pixel 5只有XL版本 搭載驍龍765G且將比Pixel 4更便宜 [圖]Wear OS將迎來重磅更新:應用啟動時間 ......

    uj5u.com 2020-09-10 07:01:38 more
  • 海豚星空掃碼投屏 Android 接收端 SDK 集成 六步驟

    掃碼投屏,開放網路,獨占設備,不需要額外下載軟體,微信掃碼,發現設備。支持標準DLNA協議,支持倍速播放。視頻,音頻,圖片投屏。好點意思。還支持自定義基于 DLNA 擴展的操作動作。好像要收費,沒體驗。 這里簡單記錄一下集成程序。 一 跟目錄的build.gradle添加私有mevan倉庫 mave ......

    uj5u.com 2020-09-10 07:01:43 more
最新发布
  • 歡迎頁輪播影片

    如圖,引導開始,球從上落下,同時淡入文字,然后文字開始輪播,最后一頁時停止,點擊進入首頁。 在來看看效果圖。 重力球先不講,主要歡迎輪播簡單實作 首先新建一個類 TextTranslationXGuideView,用于影片展示 文本是類似的,最后會有個圖片箭頭影片,布局很簡單,就是一個 TextVi ......

    uj5u.com 2023-04-20 08:40:31 more
  • 【FAQ】關于華為推送服務因營銷訊息頻次管控導致服務通訊類訊息

    一. 問題描述 使用華為推送服務下發IM訊息時,下發訊息請求成功且code碼為80000000,但是手機總是收不到訊息; 在華為推送自助分析(Beta)平臺查看發現,訊息發送觸發了頻控。 二. 問題原因及背景 2023年1月05日起,華為推送服務對咨詢營銷類訊息做了單個設備每日推送數量上限管理,具體 ......

    uj5u.com 2023-04-20 08:40:11 more
  • 歡迎頁輪播影片

    如圖,引導開始,球從上落下,同時淡入文字,然后文字開始輪播,最后一頁時停止,點擊進入首頁。 在來看看效果圖。 重力球先不講,主要歡迎輪播簡單實作 首先新建一個類 TextTranslationXGuideView,用于影片展示 文本是類似的,最后會有個圖片箭頭影片,布局很簡單,就是一個 TextVi ......

    uj5u.com 2023-04-20 08:39:36 more
  • 【FAQ】關于華為推送服務因營銷訊息頻次管控導致服務通訊類訊息

    一. 問題描述 使用華為推送服務下發IM訊息時,下發訊息請求成功且code碼為80000000,但是手機總是收不到訊息; 在華為推送自助分析(Beta)平臺查看發現,訊息發送觸發了頻控。 二. 問題原因及背景 2023年1月05日起,華為推送服務對咨詢營銷類訊息做了單個設備每日推送數量上限管理,具體 ......

    uj5u.com 2023-04-20 08:39:13 more
  • iOS從UI記憶體地址到讀取成員變數(oc/swift)

    開發除錯時,我們發現bug時常首先是從UI顯示發現例外,下一步才會去定位UI相關連的資料的。XCode有給我們提供一系列debug工具,但是很多人可能還沒有形成一套穩定的除錯流程,因此本文嘗試解決這個問題,順便提出一個暴論:UI顯示例外問題只需要兩個步驟就能完成定位作業的80%: 定位例外 UI 組 ......

    uj5u.com 2023-04-19 09:16:23 more
  • FIDE重磅更新!性能飛躍!體驗有禮!

    FIDE 開發者工具重構升級啦!實作500%性能提升,誠邀體驗! 一直以來不少開發者朋友在社區反饋,在使用 FIDE 工具的程序中,時常會遇到諸如加載不及時、代碼預覽/渲染性能不如意的情況,十分影響開發體驗。 作為技術團隊,我們深知一件趁手的開發工具對開發者的重要性,因此,在2023年開年,FinC ......

    uj5u.com 2023-04-19 09:16:15 more
  • 游戲內嵌社區服務開放,助力開發者提升玩家互動與留存

    華為 HMS Core 游戲內嵌社區服務提供快速訪問華為游戲中心論壇能力,支持玩家直接在游戲內瀏覽帖子和交流互動,助力開發者擴展內容生產和觸達的場景。 一、為什么要游戲內嵌社區? 二、游戲內嵌社區的典型使用場景 1、游戲內打開論壇 您可以在游戲內繪制論壇入口,為玩家提供沉浸式發帖、瀏覽、點贊、回帖、 ......

    uj5u.com 2023-04-19 09:15:46 more
  • iOS從UI記憶體地址到讀取成員變數(oc/swift)

    開發除錯時,我們發現bug時常首先是從UI顯示發現例外,下一步才會去定位UI相關連的資料的。XCode有給我們提供一系列debug工具,但是很多人可能還沒有形成一套穩定的除錯流程,因此本文嘗試解決這個問題,順便提出一個暴論:UI顯示例外問題只需要兩個步驟就能完成定位作業的80%: 定位例外 UI 組 ......

    uj5u.com 2023-04-19 09:14:53 more
  • FIDE重磅更新!性能飛躍!體驗有禮!

    FIDE 開發者工具重構升級啦!實作500%性能提升,誠邀體驗! 一直以來不少開發者朋友在社區反饋,在使用 FIDE 工具的程序中,時常會遇到諸如加載不及時、代碼預覽/渲染性能不如意的情況,十分影響開發體驗。 作為技術團隊,我們深知一件趁手的開發工具對開發者的重要性,因此,在2023年開年,FinC ......

    uj5u.com 2023-04-19 09:14:08 more
  • 游戲內嵌社區服務開放,助力開發者提升玩家互動與留存

    華為 HMS Core 游戲內嵌社區服務提供快速訪問華為游戲中心論壇能力,支持玩家直接在游戲內瀏覽帖子和交流互動,助力開發者擴展內容生產和觸達的場景。 一、為什么要游戲內嵌社區? 二、游戲內嵌社區的典型使用場景 1、游戲內打開論壇 您可以在游戲內繪制論壇入口,為玩家提供沉浸式發帖、瀏覽、點贊、回帖、 ......

    uj5u.com 2023-04-19 09:08:34 more