当前位置:网站首页>计算属性普通函数写法 和 set get 写法
计算属性普通函数写法 和 set get 写法
2022-07-02 06:20:00 【沉迷...】
特点:
- 定义的时候,要被定义为“方法”
- 在使用计算属性的时候,当普通的属性使用即可
- 计算属性会缓存计算的结果,只有计算属性依赖的数据变化时,才会重新进行运算
好处:
- 实现了代码的复用
- 只要计算属性中依赖的数据源变化了,则计算属性会自动重新求值!
普通写法
<template>
<div>
<p>和为: {
{ num }}</p>
<p>{
{ reverseMessage }}</p>
</div>
</template>
<script> export default {
data() {
return {
a: 10, b: 20, message: "我是个字符串", }; }, computed: {
num() {
return this.a + this.b; }, reverseMessage() {
return this.message.split("").reverse().join(""); }, }, }; </script>
复杂写法
<template>
<div>
<div>性别是:{
{
getsex}}</div>
<button @click="addarr">添加</button>
<div>改变name的值 {
{
getname}}</div>
</div>
</template>
<script>
export default {
name: "newsdetail",
data(){
return{
sex:['男','女'],
first: 'wang',
two: 'kun'
}
},
computed: {
// 每个计算属性都包括一个getter(get)和一个setter(set)
//在计算属性内填写的函数相当于简写的get
getsex:function (){
//计算属性函数中的return不能少
return this.sex[0]
},
getname:{
get: function () {
console.log('调用了fullName的get方法');
return this.first + ' ' + this.two;
},
//只有当getname中的值改变的时候才能触发set,传值的value是getname改变的值
set: function (value) {
console.log('调用了fullName的set方法',value);
const names = value.split(' ');
this.first = names[0];
this.two= names[1];
}
}
},
methods:{
addarr:function (){
this.getname='jiang ke'
}
}
}
</script>
<style scoped>
</style>
边栏推荐
- Replace Django database with MySQL (attributeerror: 'STR' object has no attribute 'decode')
- From design delivery to development, easy and efficient!
- 代码技巧——Controller参数注解@RequestParam
- sudo提权
- New version of dedecms collection and release plug-in tutorial tool
- 队列(线性结构)
- Linked list (linear structure)
- LeetCode 90. Subset II
- Detailed explanation of BGP message
- Invalid operation: Load into table ‘sources_ orderdata‘ failed. Check ‘stl_ load_ errors‘ system table
猜你喜欢

Support new and old imperial CMS collection and warehousing tutorials

IDEA公布全新默认UI,太清爽了(内含申请链接)

Zhuanzhuanben - LAN construction - Notes

Cglib代理-代码增强测试

In depth understanding of JUC concurrency (II) concurrency theory

实习生跑路留了一个大坑,搞出2个线上问题,我被坑惨了

In depth understanding of JUC concurrency (I) what is JUC

深入学习JVM底层(五):类加载机制
![Data science [9]: SVD (2)](/img/2c/f1a8c3ff34ff3f3cc6e26157a32bfd.png)
Data science [9]: SVD (2)

穀歌出海創業加速器報名倒計時 3 天,創業人闖關指南提前收藏!
随机推荐
Linked list (linear structure)
Browser principle mind map
程序员的自我修养—找工作反思篇
深入了解JUC并发(二)并发理论
代码技巧——Controller参数注解@RequestParam
Find the highest value of the current element Z-index of the page
重载全局和成员new/delete
Contest3145 - the 37th game of 2021 freshman individual training match_ H: Eat fish
Hydration failed because the initial UI does not match what was rendered on the server.问题原因之一
Learn about various joins in SQL and their differences
Mech 3002 explanation
锐捷EBGP 配置案例
Redis——缓存击穿、穿透、雪崩
Redis——Cluster数据分布算法&哈希槽
TensorRT的命令行程序
BGP中的状态机
介绍两款代码自动生成器,帮助提升工作效率
From design delivery to development, easy and efficient!
Sentinel规则持久化到Nacos
Amazon AWS data Lake Work Pit 1