03-Vue常用指令(二)

一、Vue常用指令

  • v-once 也是雙向數(shù)據(jù)綁定,但數(shù)據(jù)只綁定一次
  • v-pre 原樣輸出
  • v-bind 綁定屬性 v-bind:屬性='值'
  • v-text 單向數(shù)據(jù)綁定,不可識別標(biāo)簽
  • v-html 單向數(shù)據(jù)綁定,可以識別標(biāo)簽
1.v-once數(shù)據(jù)只綁定一次
<body>
    <div id='itany'>
        <input type="text" name="" v-model='msg'>
        <p>{{msg}}</p>
        <p v-once>{{msg}}</p>
    </div>
<script src='js/vue.js'></script>
<script type="text/javascript">
    new Vue({
        el:'#itany',
        data:{
            msg:'welcom to vue'
        }
    })
</script>

使用v-once綁定的元素中的內(nèi)容不會隨著input中輸入的內(nèi)容變化而變化,始終保持最初始的樣子

2.v-pre原樣輸出
<body>
    <div id='itany'>
        <input type="text" name="" v-model='msg'>
        <p v-pre>{{msg}}</p>
    </div>
<script src='js/vue.js'></script>
<script type="text/javascript">
    new Vue({
        el:'#itany',
        data:{
            msg:''
        }
    })
</script>
</body>

使用v-pre的標(biāo)簽之間的內(nèi)容不會進(jìn)行編譯,而是兩標(biāo)簽之間寫什么就會輸出什么

3.v-bind:綁定屬性
<body>
    <div id='itany'>
        <img v-bind:src="url">
              <img :src="url">//簡寫
    </div>
<script src='js/vue.js'></script>
<script type="text/javascript">
    new Vue({
        el:'#itany',
        data:{
            url:'img/1.jpg'
        }
    })
</script>
</body>

v-bind綁定屬性,v-bind:屬性名=‘值’,也可以省略v-bind,直接在屬性前面加冒號:, :屬性名=‘值’

4.綁定class屬性:綁定class屬性一般使用json的方式
<head>
    <meta charset="utf-8">
    <title></title>
    <style type="text/css">
        .aaa{
            color:red;
        }
        .bbb{
            background:blue;
        }
    </style>
</head>
<body>
    <div id='itany'>
               //綁定屬性
        <p v-bind:class={aaa:true,bbb:true}>hello vue.js</p>
    </div>
<script src='js/vue.js'></script>
<script type="text/javascript">
    new Vue({
        el:'#itany',
        data:{
            url:'img/1.jpg'
        }
    })
</script>
</body>

我們想使用哪個屬性就設(shè)置哪個屬性為true就可以了,如果不想使用哪個屬性,那設(shè)置它的值為false就可以了

5.v-text / v-html 單向數(shù)據(jù)綁定
<body>
    <div id='itany'>
        <input type="text" name="" v-model='msg'>
        <p v-text='msg'>{{msg}}</p>
        <p v-html='msg'>{{msg}}</p>
    </div>
<script src='js/vue.js'></script>
<script type="text/javascript">
    new Vue({
        el:'#itany',
        data:{
            msg:'scsa'
        }
    })
</script>
</body>

單向數(shù)據(jù)綁定有兩種方式
方式一:使用v-text v-html
方式二:使用兩對大括號{{}},可能會出現(xiàn)閃爍問題,可以使用v-cloak解決

v-cloak
<body>
    <div id='itany'>
        <p>{{msg}}</p>
    </div>
<script src='js/vue.js'></script>
<script type="text/javascript">
    new Vue({
        el:'#itany',
        data:{
            msg:"hello vue"
        },
        created:function(){
            alert(111)
        }
    })
</script>
</body>

如果我們想在vue實例創(chuàng)建的時候,執(zhí)行一段代碼,然后再把msg編譯到頁面上,但是我們會發(fā)現(xiàn)在執(zhí)行聲明周期中的東西是,msg還沒有被編譯完也會原樣輸出(有兼容性問題,建議多個瀏覽器去測試),為了不讓這種情況出現(xiàn),我們必須使用v-cloak

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title></title>
    <style type="text/css">
        [v-cloak]{//屬性選擇器,使用v-cloak必須加上這個樣式
            display: none;
        }
    </style>
</head>
<body>
    <div id='itany'>
        <p v-cloak>{{msg}}</p>
    </div>
<script src='js/vue.js'></script>
<script type="text/javascript">
    new Vue({
        el:'#itany',
        data:{
            msg:"hello vue"
        },
        created:function(){
            alert(111)
        }
    })
</script>
</body>
</html>
6.練習(xí):完成如下圖所示的購物車練習(xí)

1.png

代碼如下:

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>購物車</title>
    <link rel="stylesheet" type="text/css" href="css/bootstrap.css">
</head>
<body>
<div id='itany'>    
     <h1>總價:¥{{total}}</h1>
     <table class='table table-bordered table-straped text-center'>
        <thead>
            <tr>
                <th class='text-center'>編號</th>
                <th class='text-center'>名稱</th>
                <th class='text-center'>單價</th>
                <th class='text-center'>數(shù)量</th>
                <th class='text-center'>總價</th>
            </tr>
        </thead>
        <tbody>
            <tr v-for="(val,index) in fruitList">
                <td>{{index+1}}</td>
                <td>{{val.name}}</td>
                <td>{{val.price}}</td>
                <td>
                   <button @click='add(index)'>+</button>
                   <span>{{val.num}}</span>
                   <button @click='redu(index)'>-</button>
                </td>
                <td>{{val.sub}}</td>
            </tr>
        </tbody>
     </table>
</div>
<script src='js/vue.js'></script>
<script type="text/javascript">
    new Vue({
        el:'#itany',
        data:{
            fruitList:[
                {name:'香蕉',price:2.5,num:2,sub:5},
                {name:'蘋果',price:3.5,num:3,sub:10.5},
                {name:'鴨梨',price:4.5,num:4,sub:18}
            ],
            total:0
        },
        methods:{
            add:function(index){
                 this.fruitList[index].num++;
                 this.fruitList[index].sub=Number(this.fruitList[index].num)*Number(this.fruitList[index].price);
                 //調(diào)用總價
                 this.toal(index);
            },
            redu:function(index){
                if(this.fruitList[index].num>1){
                    this.fruitList[index].num--;
                    this.fruitList[index].sub=Number(this.fruitList[index].num)*Number(this.fruitList[index].price);
                }

                //調(diào)用總價
                 this.toal();
            },
            toal:function(){
                 for(var i=0,sum=0;i<this.fruitList.length;i++){
                    sum+=this.fruitList[i].sub;
                    this.total=sum.toFixed(2);
                 }
            }
            
        }
        
    })
</script>
</body>
</html>
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

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

  • vue概述 在官方文檔中,有一句話對Vue的定位說的很明確:Vue.js 的核心是一個允許采用簡潔的模板語法來聲明...
    li4065閱讀 7,624評論 0 25
  • Spring Cloud為開發(fā)人員提供了快速構(gòu)建分布式系統(tǒng)中一些常見模式的工具(例如配置管理,服務(wù)發(fā)現(xiàn),斷路器,智...
    卡卡羅2017閱讀 136,568評論 19 139
  • 原文 博客原文 大綱 1、什么是vue指令2、向指令中傳入?yún)?shù)3、指令中帶入修飾符4、指令的縮寫5、常見的vue指...
    前端路上的小兵閱讀 494評論 0 8
  • 這篇筆記主要包含 Vue 2 不同于 Vue 1 或者特有的內(nèi)容,還有我對于 Vue 1.0 印象不深的內(nèi)容。關(guān)于...
    云之外閱讀 5,177評論 0 29
  • 小灰灰不知道什么時候睡著的,醒來的時候已經(jīng)是夕陽西下了。他伸了個懶腰,還是忘不掉那個夢。這時的小黑已經(jīng)拿出吃的來了...
    小米1991閱讀 541評論 0 0

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