Vue內(nèi)部指令

1.v-if/v-else/v-show

        <div v-if="isLogin">isLogin為true時(shí)顯示</div>
        <div v-else>isLogin為false時(shí)顯示</div>
        <div v-show="isLogin">isLogin為true時(shí)顯示</div>
        <!-- 
        v-if與v-show的區(qū)別
        v-if:判斷是否加載,可以減輕服務(wù)器的壓力,需要時(shí)加載
        v-show:調(diào)整css display屬性,可以使客戶端操作更加流暢。
         -->

2.v-for

        <ul>
            <li v-for="item in items">
                {{item}}
            </li>
        </ul>

3.v-text/v-html

<body>
    <h1>v-text&v-html<h1>
    <hr>
    <div id="app">
        <!-- 弊端:js出錯(cuò)時(shí),頁面顯示{{message}}錯(cuò)誤內(nèi)容 -->
        <span>{{message}}</span>
    
        <!-- v-text:js沒內(nèi)容時(shí)頁面不顯示 -->
        <span v-text="dodo"></span>

        <!-- v-html可以解析標(biāo)簽,弊端:可能引起攻擊,盡量少用。 -->
        <span v-html="dodo"> </span>
    </div>

    <script type="text/javascript">
        var app = new Vue({
            el:'#app',
            data:{
                message:'hello world!',
                dodo:'<h2>hello world!</h2>'
            }
        })
    </script>
</body>

4.v-on監(jiān)聽事件綁定

<body>
    <div id="app">
        <p>比賽得分:{{score}}</p>
        <p>
            <button v-on:click="addScore">加分(v-on)</button>
            <!-- v-on簡寫方法 -->
            <button @click="substractScore">減分(v-on簡寫)</button>
            <br/>
            <!-- 鍵盤碼 enter對(duì)應(yīng)13 可以使用v-on:keyup.13 -->
            <input type="text" v-on:keyup.enter="onEnter" v-model="score2">

            <button onclick="onclickAdd()">加分(onclick方法)</button>
        </p>
    </div>

    <script type="text/javascript">

        function onclickAdd(){
            app.score += 2;//數(shù)據(jù)處理和app中的不同
        };

        var app = new Vue({
            el:'#app',
            data:{
                score:0,
                score2:1,//綁定初始值
            },
            methods:{
                addScore:function(){
                    this.score ++;
                },
                substractScore:function(){
                    this.score --;
                },
                onEnter:function(){
                    this.score = this.score + parseInt(this.score2);
                },
            }
        })
    </script>
</body>

5.v-model數(shù)據(jù)源綁定、雙向數(shù)據(jù)綁定

        <p>原始文本信息:{{message}}</p>
        
        <p>v-model:<input type="text" v-model="message"></p>

        <!-- lazy:文本框失去焦點(diǎn)時(shí)更新數(shù)據(jù) -->
        <p>v-model.lazy:<input type="text" v-model.lazy="message"></p>

        <!-- number:先輸入數(shù)字再輸入文本,文本不顯示 -->
        <p>v-model.number:<input type="text" v-model.number="message"></p>

        <!-- trim:去掉空格 -->
        <p>v-model.trim<input type="text" v-model.trim="message"></p>

6.v-bind綁定標(biāo)簽上的屬性

<div id="app">
        <img v-bind:src="imgSrc" width="200px"/>
        <!-- 簡寫方法 -->
        <a :href="webUrl" target="_blank">技術(shù)宅</a>

        <div :class="className">1.綁定Class</div>
        <div :class="{classA:isOK}">2.綁定Class中的判斷</div>
        <div :class="[classA,classB]">3.綁定Class中數(shù)組</div>
        <div :class="isOK?classA:classB">4.綁定Class中三元運(yùn)算符</div>

        <div>
            <input type="checkbox" id="isOK" v-model="isOK">
            <label for="isOK">isOK={{isOK}}</label>
        </div>

    <div :style="{color:red,fontSize:font}">5.綁定style值</div>
    <div :style="styleObject">6.對(duì)象綁定style</div>
    </div>

7.v-pre/v-cloak/v-once

<div id="app">
        
        <div v-pre>v-pre 原樣輸出,不進(jìn)行渲染:{{message}}</div>

        <div v-cloak>v-cloak渲染完成后才顯示:{{message}}</div>

        <div v-once>v-once只在第一次渲染時(shí)顯示一次:{{message}}</div>

        <div><input type="text" v-model="message">修改內(nèi)容對(duì)v-once不影響</div>
    </div>
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 指令( Directives )是 Vue 模板中最常用的一項(xiàng)功能,它帶有前綴 v-,能幫我們快速完成DOM操作,...
    苦瓜_6閱讀 324評(píng)論 0 0
  • 內(nèi)部指令 v-text 讀取文本不能讀取html標(biāo)簽,解析文本使用{{XXX}}這種情況是有弊端的,當(dāng)我們網(wǎng)速...
    九瀺閱讀 269評(píng)論 0 0
  • 本節(jié)知識(shí)點(diǎn) 內(nèi)部知識(shí)點(diǎn)匯總 內(nèi)部指令 v-if="xxx" v-else v-for="(內(nèi)容,序號(hào)) in 數(shù)組...
    我擁抱著我的未來閱讀 234評(píng)論 0 0
  • 什么是vue html: js: 1、v-if v-else-if v-else 2、v-show 注:v-i...
    魏曉嵐恬閱讀 1,469評(píng)論 0 1
  • 看見一個(gè)友人在朋友圈發(fā)布她結(jié)婚紀(jì)念日照片,想起,我今年的紀(jì)念日貌似錯(cuò)過了呢!93年結(jié)婚,至今已經(jīng)23年...
    五六魚兒閱讀 268評(píng)論 0 1

友情鏈接更多精彩內(nèi)容