关于vue 常用指令,以及动态添加样式,单选效果,多选效果样式实现

1.vue常用指令

1.v-once
能执行一次性地插值,当数据改变时,插值处的内容不会更新。但请留心这会影响到该节点上的其它数据绑定

2.v-show
和v-if一样 区别是if是注释掉 v-show是给一个display:none的属性 让它不显示! 用法 参考下一个v-if指令.

3.v-if
v-if后面的是一个表达式或者也可以是返回true或false的表达式。 且值为true和fasle false的话就会被注释 v-show是给一个display:none的属性 让它不显示! true就正常显示。

4.v-else
必须和v-if一起用才可以 不能单独用 而且必须在v-if下面 中间有别的标签也会报错

5.v-for
基于数据渲染一个列表,类似于JS中的遍历。其数据类型可以是 Array | Object | number | string
该指令之值,必须使用特定的语法(item, index) in items, index是索引,可以省略。item是 为当前遍历元素提供别名(可以自己随便起名字) 。v-for的优先级别高于v-if之类的其他指令

7.v-html
双大括号会将数据解释为普通文本,而非 HTML 代码。为了输出真正的 HTML,你需要使用 v-html 而且给一个标签加了v-html 里面包含的标签都会被覆盖。

8.v-text
给一个便签加了v-text 会覆盖标签内部原先的内容

9.v-bind
绑定属性,可以简写为":"

10.10.v-on
绑定事件,可以简写为"@"

11.v-model
v-model是一个指令,限制在、、、components中使用 用于数据的双向绑定操作.

2.动态添加,切换样式

动态绑定style

<template>
  <div>
    <div :style="{ color: a }">哈哈哈</div>
   <!-- <div:style="{样式的属性名:变量}">哈哈哈</div>-->
  </div>
</template>

<script>
export default {
  data() {
    return {
      a: "red",
    };
  },
};
</script>

<style>
</style>

动态绑定calss

<template>
  <div :class="{ active: isActive }">123</div>
    <!-- <div :class="{类名:变量}">123</div> -->
</template>

<script>
export default {
  data() {
    return {
      isActive: true,
    };
  },
};
</script>

<style>
.active {
  color: red;
  background: pink;
}
</style>

单选效果

<template>
  <div class="aaa">
    <div
      v-for="(item, index) in list"
      :key="index"
      :class="item.isActive ? 'active' : 'actives'"
      @click="onClick(index)"
    >
      {{ item.title }}
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      list: [
        { title: 1, isActive: false },
        { title: 2, isActive: false },
        { title: 3, isActive: false },
        { title: 4, isActive: false },
        { title: 5, isActive: false },
        { title: 6, isActive: false },
      ],
    };
  },
  methods: {
    onClick(index) {
      this.list.forEach((item) => {
        item.isActive = false;
      });
      this.list[index].isActive = true;
    },
  },
};
</script>

<style>
.aaa {
  width: 100%;
  display: flex;
  align-items: center;
  justify-content: center;
  word-wrap: wrap;
}
.active {
  width: 5%;
  height: 50px;
  background: pink;
  border: 1px solid gray;
  margin-left: 10px;
}
.actives {
  width: 5%;
  height: 50px;
  border: 1px solid gray;
  margin-left: 10px;
  background: blue;
}
</style>

在这里插入图片描述
在这里插入图片描述

多选效果

<template>
  <div class="aaa">
    <div
      v-for="(item, index) in list"
      :key="index"
      :class="item.isActive ? 'active' : 'actives'"
      @click="onClick(index)"
    >
      {{ item.title }}
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      list: [
        { title: 1, isActive: true },
        { title: 2, isActive: true },
        { title: 3, isActive: true },
        { title: 4, isActive: true },
        { title: 5, isActive: true },
        { title: 6, isActive: true },
      ],
    };
  },
  methods: {
    onClick(index) {
      console.log(index);
      this.list[index].isActive = !this.list[index].isActive;
    },
  },
};
</script>

<style>
.aaa {
  width: 100%;
  display: flex;
  align-items: center;
  justify-content: center;
  word-wrap: wrap;
}
.active {
  width: 5%;
  height: 60px;
  background: pink;
  border: 1px solid gray;
  margin-left: 10px;
}
.actives {
  width: 5%;
  height: 60px;
  border: 1px solid gray;
  margin-left: 10px;
  background: red;
}
</style>

在这里插入图片描述

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值