Vue的基本使用案例

Vue的基本使用案例

1.当鼠标经过一行时显示不一样的颜色

<style>
  .active{
    background: red;
  }
</style>
<div id="app">
  <ul>
    <!--    这样只能拿到属性的value,不能拿到key-->
    <li v-for="(name, index) in names" :key="name" :class="{active : showActive(index)}"
    @mouseover="setCurrentIndex(index)">{{name}}</li>
  </ul>
</div>
<script>
    const vue = new Vue({
        el: '#app',
        data: {
            names: ['张三', '李四', '纪律委员','111', '222', '333'],
            currentIndex: 0,
        },
        methods: {
            changed() {
                this.names.splice(0, 1, '111');
                // vue 内部实现的方法
                Vue.set(this.names, 1, '222');
            },
            setCurrentIndex(index) {
                this.currentIndex = index
            },
            showActive(index) {
                return this.currentIndex === index;
            }
        }
    });

</script>

在这里插入图片描述

2. 购物车页面

  <style>
    table {
      border: 1px solid #e9e9e9;
      border-collapse: collapse;
      border-spacing: 0;
    }
    th, td {
      padding: 8px 16px;
      border: 1px solid #e9e9e9;
      text-align: center;
    }
    th {
      background-color: #f7f7f7;
      color: #5c6b77;
      font-weight: 600;
    }
  </style>
  <div id="app">
    <table>
      <tr>
        <th></th>
        <th>商品名称</th>
        <th>上架时间</th>
        <th>价格</th>
        <th>购买数量</th>
        <th>操作</th>
      </tr>
      <tr v-for="(good, index) in goods" :key="good.id">
        <td v-text="good.id"></td>
        <td v-text="good.name"></td>
        <td v-text="good.importDate"></td>
        <td >{{good.price | formatPrice}}</td>
        <td>
          <button @click="decrement(index, good.num)">-</button>
          {{good.num}}
          <button @click="increment(index, good.num)">+</button>
        </td>
        <td>
          <button @click="removeCargo(index)">移除</button>
        </td>
      </tr>
    </table>
    <b>总计:</b>
    <span v-text="totalPrice"></span>
  </div>
  <script>
    const vue = new Vue({
        el: '#app',
        data: {
            goods: [
                {id: 1, name: '华为手机', importDate: '2006-10', price: 2000, num: 1},
                {id: 2, name: '小米手机', importDate: '2006-11', price: 1900, num: 1},
                {id: 3, name: 'OPPO手机', importDate: '2006-12', price: 1800, num: 1},
                {id: 4, name: 'VIVO手机', importDate: '2007-01', price: 1700, num: 1},
                {id: 5, name: '魅族手机', importDate: '2007-02', price: 1600, num: 1},
            ]
        },
        methods: {
            decrement(index, num) {
                var goodNum = --this.goods[index].num;
                if (goodNum === 0){
                    this.removeCargo(index);
                }
            },
            increment(index, num) {
                this.goods[index].num++;
            },
            removeCargo (index) {
                this.goods.splice(index, 1);
            }
        },
        computed: {
            totalPrice() {
                let totalPrice = 0;
                const cargos = this.goods;
                for (let i = 0; i < cargos.length; i++){
                    totalPrice += (cargos[i].price * cargos[i].num);
                }
                return totalPrice.toFixed(2);
            }
        },
        filters: {
            formatPrice(price) {
                return '¥ ' + price.toFixed(2);
            }
        }
    });
  </script>

在这里插入图片描述

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值