05 vue3之computed用法

2024-08-28 07:12

本文主要是介绍05 vue3之computed用法,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

computed用法

计算属性就是当依赖的属性的值发生变化的时候,才会触发他的更改,如果依赖的值,不发生变化的时候,使用的是缓存中的属性值。

1 函数形式

let name = computed(() => {return one.value + "---" + two.value; // 取值一定要加.value
});

2 对象形式

let name1 = computed({get() {return one.value + "---" + two.value;},set() {one.value + "---" + two.value;},
});

完整案例

<template><input v-model="one" type="text" /><input v-model="two" type="text" /><!-- <div>{{ one }}---{{ two }}</div> --><div>{{ name }}</div><div>{{ name1 }}</div></template><script setup lang="ts">
import { ref, reactive, computed } from "vue";
let one = ref("");
let two = ref("");
// 1种写法 函数式写法 不允许修改值
let name = computed(() => {return one.value + "---" + two.value; // 取值一定要加.value
});
// 2种写法 选项式写法 允许修改值
let name1 = computed({get() {return one.value + "---" + two.value;},set() {one.value + "---" + two.value;},
});</script><style scoped>
table,
td {border: 1px solid #333;
}thead,
tfoot {background-color: #333;color: #fff;
}
</style>

购物车案例:

能优化代码 减少多次调用

<template><table><inputplaceholder="请输入名称"v-model="keyWord"type="text"/><thead><tr><!-- <th colspan="2">The table header</th> --><th>名称</th><th>数量</th><th>价格</th><th>操作</th></tr></thead><tbody><tr v-for="item in searchData" :key="item"><td>{{ item.name }}</td><td><button @click="add(item, true)">+</button>{{ item.num}}<button @click="add(item, false)">-</button></td><td>{{ item.price * item.num }}</td><td><button @click="del(index)">删除</button></td></tr></tbody><tfoot><td></td><td></td><td></td><td>总价:{{ $total }}</td></tfoot></table>
</template><script setup lang="ts">
import { ref, reactive, computed } from "vue";
type shop = {name: string;num: number;price: number;
};
let keyWord = ref("");// let $total = ref<string>("0");
let data = reactive<shop[]>([{ name: "裤子", num: 1, price: 100 },{ name: "衣服", num: 1, price: 200 },{ name: "鞋子", num: 1, price: 300 },
]);
const searchData = computed(() => {return data.filter((item) =>item.name.includes(keyWord.value));
});let add = (item: shop, type: Boolean): void => {if (item.num < 99 && type) {item.num++;// total();}if (item.num > 1 && !type) {item.num--;// total();}
};
let del = (index: number) => {data.splice(index, 1);// total();
};/* const total = () => {$total.value = data.reduce((pre, next) => {return pre + next.price * next.num;}, 0);
}; */
// total();// 可以看到total被调用了很多次 我们可以使用computed优化代码
const $total = computed(() => {return data.reduce((pre, next) => {return pre + next.price * next.num;}, 0);
});
</script><style scoped>
table,
td {border: 1px solid #333;
}thead,
tfoot {background-color: #333;color: #fff;
}
</style>

这篇关于05 vue3之computed用法的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1114084

相关文章

spring-boot-starter-thymeleaf加载外部html文件方式

《spring-boot-starter-thymeleaf加载外部html文件方式》本文介绍了在SpringMVC中使用Thymeleaf模板引擎加载外部HTML文件的方法,以及在SpringBoo... 目录1.Thymeleaf介绍2.springboot使用thymeleaf2.1.引入spring

部署Vue项目到服务器后404错误的原因及解决方案

《部署Vue项目到服务器后404错误的原因及解决方案》文章介绍了Vue项目部署步骤以及404错误的解决方案,部署步骤包括构建项目、上传文件、配置Web服务器、重启Nginx和访问域名,404错误通常是... 目录一、vue项目部署步骤二、404错误原因及解决方案错误场景原因分析解决方案一、Vue项目部署步骤

JavaScript中的reduce方法执行过程、使用场景及进阶用法

《JavaScript中的reduce方法执行过程、使用场景及进阶用法》:本文主要介绍JavaScript中的reduce方法执行过程、使用场景及进阶用法的相关资料,reduce是JavaScri... 目录1. 什么是reduce2. reduce语法2.1 语法2.2 参数说明3. reduce执行过程

前端原生js实现拖拽排课效果实例

《前端原生js实现拖拽排课效果实例》:本文主要介绍如何实现一个简单的课程表拖拽功能,通过HTML、CSS和JavaScript的配合,我们实现了课程项的拖拽、放置和显示功能,文中通过实例代码介绍的... 目录1. 效果展示2. 效果分析2.1 关键点2.2 实现方法3. 代码实现3.1 html部分3.2

Python itertools中accumulate函数用法及使用运用详细讲解

《Pythonitertools中accumulate函数用法及使用运用详细讲解》:本文主要介绍Python的itertools库中的accumulate函数,该函数可以计算累积和或通过指定函数... 目录1.1前言:1.2定义:1.3衍生用法:1.3Leetcode的实际运用:总结 1.1前言:本文将详

MyBatis-Flex BaseMapper的接口基本用法小结

《MyBatis-FlexBaseMapper的接口基本用法小结》本文主要介绍了MyBatis-FlexBaseMapper的接口基本用法小结,文中通过示例代码介绍的非常详细,对大家的学习或者工作具... 目录MyBATis-Flex简单介绍特性基础方法INSERT① insert② insertSelec

CSS弹性布局常用设置方式

《CSS弹性布局常用设置方式》文章总结了CSS布局与样式的常用属性和技巧,包括视口单位、弹性盒子布局、浮动元素、背景和边框样式、文本和阴影效果、溢出隐藏、定位以及背景渐变等,通过这些技巧,可以实现复杂... 一、单位元素vm 1vm 为视口的1%vh 视口高的1%vmin 参照长边vmax 参照长边re

CSS3中使用flex和grid实现等高元素布局的示例代码

《CSS3中使用flex和grid实现等高元素布局的示例代码》:本文主要介绍了使用CSS3中的Flexbox和Grid布局实现等高元素布局的方法,通过简单的两列实现、每行放置3列以及全部代码的展示,展示了这两种布局方式的实现细节和效果,详细内容请阅读本文,希望能对你有所帮助... 过往的实现方法是使用浮动加

css渐变色背景|<gradient示例详解

《css渐变色背景|<gradient示例详解》CSS渐变是一种从一种颜色平滑过渡到另一种颜色的效果,可以作为元素的背景,它包括线性渐变、径向渐变和锥形渐变,本文介绍css渐变色背景|<gradien... 使用渐变色作为背景可以直接将渐China编程变色用作元素的背景,可以看做是一种特殊的背景图片。(是作为背

CSS自定义浏览器滚动条样式完整代码

《CSS自定义浏览器滚动条样式完整代码》:本文主要介绍了如何使用CSS自定义浏览器滚动条的样式,包括隐藏滚动条的角落、设置滚动条的基本样式、轨道样式和滑块样式,并提供了完整的CSS代码示例,通过这些技巧,你可以为你的网站添加个性化的滚动条样式,从而提升用户体验,详细内容请阅读本文,希望能对你有所帮助...