05-Vue过滤器

本文演示了如何在Vue.js应用中使用过滤器来格式化价格,将价格转换为带有美元符号的格式。示例代码展示了一个包含书籍信息的表格,其中价格字段通过`showPrice`过滤器进行处理,使得价格在页面上以美元形式展示。
摘要由CSDN通过智能技术生成

Vue过滤器

  • 方便面重复使用,简化html
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Title</title>
</head>
<body>

<div id="app">
  <table>
    <thead>
      <tr>
        <th></th>
        <th>出版名称</th>
        <th>出版日期</th>
        <th>价格</th>
        <th>购买数量</th>
        <th>操作</th>
      </tr>
    </thead>
    <tbody>
      <tr v-for="item in books">
        <td>{{item.id}}</td>
        <td>{{item.name}}</td>
        <td>{{item.data}}</td>
        <td>{{item.price|showPrice}}</td>
      </tr>
    </tbody>
  </table>
</div>

<script type="text/javascript" src="../js/vue.js"></script>
<script>
  const app = new Vue ({
    el: '#app',
    data: {
     books:[
       {id:1,name:'《算法争论》',data:'2006-9',price:85.00,count:1},
       {id:1,name:'《unix编程艺术》',data:'2003-1',price:95.00,count:1},
       {id:1,name:'《算法争论》',data:'2006-9',price:102.00,count:1},
       {id:1,name:'《代码大全》',data:'2005-5',price:65.00,count:1}
     ],
    },
    filters:{
      showPrice(price){
        return '$' + price.toFixed(2)
      }
    }
  })
</script>
</body>
</html>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值