Vue指令03——v-bind和v-for的使用

Vue指令03——v-bind和v-for

v-bind命令

效果:更改元素属性,如 src、title、href

格式:v-bind:属性=”变量“

格式::属性=”变量“

修改行类样式1

<!--绑定样式-->
<div id="app">
  <!-- 绑定样式属性值 -->
  <div v-bind:style="{backgroundColor:pink,width:width,height:height}">
         <!-- 绑定样式对象 -->
         <div v-bind:style=“myDiv”></div>
  </div>
</div>

<script>
var app=new Vue({
    el:"#app",
    data: {
    //变量名:值
     pink:'pink',
     width:'100%', 
     height: '200px'
     
       //字典型数据,驼峰写样式
      myDiv:{
      backgroundColor: 'red', 
      width: '100px',
      height: '100px‘ 
   }
        
}

    })
</script>
<body>
    <!--绑定样式-->
    <style>
        .box1 {
            background-color: pink;
            width: 500px;
            height: 600px;
        }

        #box2 {
            background-color: red;
            width: 100px;
            height: 100px;
        }

        .box3 {
            margin-top: 20px;
            background-color: blue;
            width: 100px;
            height: 100px;
        }
    </style>

    <div id="app" :class="B1">
        <div :id="B2"> </div>
        <div :class="B3"> </div>
    </div>

    <script>
        var app = new Vue({
            el: "#app",
            data: {
                B1: "box1",
                B2: "box2",
                B3: "box3"
            }

        })
    </script>
</body>

v-for命令

作用:自动添加元素

格式1:v-for="变量 in 数组/值"

格式2:v-for="(变量,下标变量) in 数组"

this.数组名.push(数据) //在数组最后添加数据

this.数组名.shift() //删除数组最后的数据

this.数组名.splice(下标,1); //删除数组指定的数据 ,1代表删1条

<div id="acc">
    <button @click="add">按钮+</button>
    <button @click="rm">按钮—</button>
<ul>
     <!--把数组arr的值赋给变量it,index为下标变量-->
    <li v-for="(it,index) in arr">{{index}}_{{it}}</li> 
     <!--把数组vc的值赋值给info-->
    <li v-for="info in vc">{{info.name}}</li>

</ul>

</div>
<script>
    var info=new Vue({
        el:"#acc",
        data:{
            //数组1
            arr:["好运来!","好运离开","痛苦棘手"],  
            //  数组2,值是对象
            vc:[{name:"小明"},{name:"小红"}]
        },
        methods:{
           add:function(){
               //push:在数组后添加值
            this.vc.push({name:1234})
           },
           rm:function(){
               //shift:从数组左边移出值
               this.vc.shift()
           }
        }
    })
</script>
posted @   永恒之月TEL  阅读(124)  评论(0编辑  收藏  举报
点击右上角即可分享
微信分享提示
评论
收藏
关注
推荐
深色
回顶
展开
  1. 1 Welcome Home REOL
  2. 2 Everywhere We Go REOL
  3. 3 凄美地 REOL
  4. 4 最后一页 REOL
凄美地 - REOL
00:00 / 00:00
An audio error has occurred, player will skip forward in 2 seconds.

作词 : 郭顶

作曲 : 郭顶

编曲 : 郭顶

制作人 : 郭顶

曾经我是不安河水

穿过森林误入你心

没计划扎营 搁下了是非

一去不回

如今我是造梦的人呐

怅然若失流连忘返啊

等潮汐来临 我就能记起

你的样子

我没看过

平坦山丘

怎么触摸

开花沼泽

嘿 等我找到你

试探你眼睛

心无旁骛地 相拥

那是我 仅有的温柔也是我爱你的原因

在这凄美地

曾经这里是无人之地

为何没留下有效地址

肆意的消息 迷失在十月

没有音讯

如今这里是风和日丽

等你再回来雨过迁徙

看夜幕将近 我又能记起

你的样子

我还记得

平坦山丘

如今身在

开花沼泽

嘿 等我找到你

试探你眼睛

心无旁骛地 相拥

那是我 仅有的温柔也是我爱你的原因

在这凄美地

在这之前

别说再见

我已再经不起离别

在这之前

别说再见

我已经开始了想念

在这之前

别说再见

请帮我停住这时间

就这样 别安慰

嘿 等我找到你

望住你眼睛

心无旁骛地 相拥

那是我 仅有的温柔也是我爱你的原因

如此不可及

如此不思议

让我坠落

在这凄美地