vue计算属性 初步使用案例

html 复制代码
<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>

效果展示

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

页面由下面代码决定

html 复制代码
<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()用于计算总价格

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

相关推荐
白菜__3 分钟前
去哪儿小程序逆向分析(酒店)
前端·javascript·爬虫·网络协议·小程序·node.js
前端老曹4 分钟前
Jspreadsheet CE V5 使用手册(保姆版) 二
开发语言·前端·vue.js·学习
IT_陈寒5 分钟前
SpringBoot3.0实战:5个高并发场景下的性能优化技巧,让你的应用快如闪电⚡
前端·人工智能·后端
秋邱6 分钟前
AR 定位技术深度解析:从 GPS 到视觉 SLAM 的轻量化实现
开发语言·前端·网络·人工智能·python·html·ar
老华带你飞10 分钟前
动物救助|流浪狗救助|基于Springboot+vue的流浪狗救助平台设计与实现(源码+数据库+文档)
java·数据库·vue.js·spring boot·论文·毕设·流浪动物救助平台
云飞云共享云桌面13 分钟前
佛山某机械加工设备工厂10个SolidWorks共享一台服务器的软硬件
大数据·运维·服务器·前端·网络·人工智能·性能优化
开发者小天16 分钟前
React中使用classnames的案例
前端·react.js·前端框架
简单的话*23 分钟前
Logback 日志按月归档并保留 180 天,超期自动清理的配置实践
java·前端·python
困惑阿三26 分钟前
深入理解 JavaScript 中的(Promise.race)
开发语言·前端·javascript·ecmascript·reactjs
我命由我1234528 分钟前
微信小程序 bind:tap 与 bindtap 的区别
开发语言·前端·javascript·微信小程序·小程序·前端框架·js