当前位置: 首页 > news >正文

vue计算属性 初步使用案例

<template><div><h1>购物车</h1><div v-for="item in filteredItems" :key="item.id"><p>{{ item.name }} - {{ item.price }} 元</p><input type="number" v-model.number="item.quantity" min="1" /><button @click="removeItem(item.id)">移除</button></div><div><label>筛选价格(仅仅筛选单价):<select v-model="priceFilter"><option value="all">全部</option><option value="under50">小于 50 元</option><option value="50to100">50 - 100 元</option><option value="above100">大于 100 元</option></select></label></div><h2>总价:{{ totalPrice }} 元</h2><button @click="clearCart">清空购物车</button></div>
</template><script>
export default {data() {return {cartItems: [{ id: 1, name: "商品 A", price: 30, quantity: 1 },{ id: 2, name: "商品 B", price: 60, quantity: 1 },{ id: 3, name: "商品 C", price: 120, quantity: 1 },],priceFilter: "all", // 筛选条件};},computed: {// 计算总价totalPrice() {return this.cartItems.reduce((total, item) => total + item.price * item.quantity,0);},// 根据筛选条件过滤商品filteredItems() {if (this.priceFilter === "under50") {return this.cartItems.filter((item) => item.price < 50);} else if (this.priceFilter === "50to100") {return this.cartItems.filter((item) => item.price >= 50 && item.price <= 100);} else if (this.priceFilter === "above100") {return this.cartItems.filter((item) => item.price > 100);} else {return this.cartItems;}},},methods: {// 移除商品removeItem(id) {this.cartItems = this.cartItems.filter((item) => item.id !== id);},// 清空购物车clearCart() {this.cartItems = [];},},
};
</script><style scoped>
h1,
h2 {color: #333;
}
button {margin-top: 10px;
}
</style>

效果展示

(本章节仅仅面向初步学习,页面简陋)

页面由下面代码决定

<template><div><h1>购物车</h1><div v-for="item in filteredItems" :key="item.id"><p>{{ item.name }} - {{ item.price }} 元</p><input type="number" v-model.number="item.quantity" min="1" /><button @click="removeItem(item.id)">移除</button></div><div><label>筛选价格(仅仅筛选单价):<select v-model="priceFilter"><option value="all">全部</option><option value="under50">小于 50 元</option><option value="50to100">50 - 100 元</option><option value="above100">大于 100 元</option></select></label></div><h2>总价:{{ totalPrice }} 元</h2><button @click="clearCart">清空购物车</button></div>
</template>

其中 用v-for循环进行页面打印表单。

computed是计算属性,它与data同级代码块。

这个页面,由计算属性来操控。

我们的筛选板块,

用v-model对priceFilter进行了双向数据帮当,单选每一项的时候,会改变其值。

在我们的计算属性当中 

会根据我们单项筛选,进行相应的页面展示,计算属性类似于函数,也有其返回值,返回值可以是个数组。

计算属性可以写多个 如同函数类似,

totalPrice()用于计算总价格

计算属性可用于插值表达式

http://www.lryc.cn/news/484169.html

相关文章:

  • 大模型时代,呼叫中心部门如何建设一套呼出机器人系统?
  • 使用Java绘制图片边框,解决微信小程序map组件中marker与label层级关系问题,label增加外边框后显示不能置与marker上面
  • 力扣 LeetCode 142. 环形链表II(Day2:链表)
  • 用MVVM设计模式提升WPF开发体验:分层架构与绑定实例解析
  • C++中的动态断言和静态断言
  • 运算放大器的学习(一)输入阻抗
  • Rust枚举之卧龙凤雏(Rust Option枚举、Rust Result枚举)(Rust Enum、Some(T)、Ok(T)、Err(E))链式操作
  • TCP/IP协议,TCP和UDP区别
  • 【go从零单排】Timer、Epoch 时间函数
  • 壁仞科技上市前最后一波 校招 社招 内推
  • 【微软报告:多模态基础模型】(2)视觉理解
  • Linux 驱动
  • 【数学二】线性代数-线性方程组-齐次线性方程组、非齐次线性方程组
  • Git别名设置
  • 算法基础 -- 红黑树原理与插入伪代码
  • 力扣 LeetCode 27. 移除元素(Day1:数组)
  • 微服务链路追踪skywalking安装
  • mqtt学习笔记(一)
  • Kafka Eagle 安装教程
  • Ajax 获取进度和中断请求
  • 实验5:网络设备发现、管理和维护
  • kafka 生产经验——数据积压(消费者如何提高吞吐量)
  • 对等同步身份认证(Simultaneous Authentication of Equals,简称SAE)介绍
  • Ajax 与 Vue 框架应用点——随笔谈
  • The Internals of PostgreSQL 翻译版 持续更新...
  • redis 原理篇 31 redis内存回收 内存淘汰策略
  • 微信小程序——实现二维码扫描功能(含代码)
  • 【go从零单排】HTTP客户端和服务端
  • Android 配置默认输入法
  • 交易术语汇总(Technical Trading Dictionary)