当前位置:网站首页>计算属性普通函数写法 和 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>
边栏推荐
- VRRP之监视上行链路
- 深入学习JVM底层(四):类文件结构
- Sudo right raising
- Contest3147 - game 38 of 2021 Freshmen's personal training match_ G: Flower bed
- When requesting resttemplate, set the request header, request parameters, and request body.
- Redis——大Key問題
- 深入了解JUC并发(一)什么是JUC
- 代码技巧——Controller参数注解@RequestParam
- Data playback partner rviz+plotjuggler
- Data science [9]: SVD (2)
猜你喜欢
Reading classic literature -- Suma++
最新CUDA环境配置(Win10 + CUDA 11.6 + VS2019)
It is said that Kwai will pay for the Tiktok super fast version of the video? How can you miss this opportunity to collect wool?
Google play academy team PK competition, official start!
Google Go to sea entrepreneurship accelerator registration countdown 3 days, entrepreneurs pass through the guide in advance collection!
Redis——大Key問題
Cglib代理-代码增强测试
Leverage Google cloud infrastructure and landing area to build enterprise level cloud native excellent operation capability
网络相关知识(硬件工程师)
深入了解JUC并发(二)并发理论
随机推荐
Data playback partner rviz+plotjuggler
CUDA中的异步数据拷贝
Ruijie ebgp configuration case
In depth understanding of JUC concurrency (II) concurrency theory
Redis---1. Data structure characteristics and operation
日志(常用的日志框架)
LeetCode 283. 移动零
深入学习JVM底层(二):HotSpot虚拟机对象
The Chinese word segmentation task is realized by using traditional methods (n-gram, HMM, etc.), neural network methods (CNN, LSTM, etc.) and pre training methods (Bert, etc.)
LeetCode 83. 删除排序链表中的重复元素
AttributeError: ‘str‘ object has no attribute ‘decode‘
【程序员的自我修养]—找工作反思篇二
CUDA中的Warp matrix functions
数据科学【八】:SVD(一)
I/o impressions from readers | prize collection winners list
When requesting resttemplate, set the request header, request parameters, and request body.
日期时间API详解
线性dp(拆分篇)
介绍两款代码自动生成器,帮助提升工作效率
Does the assignment of Boolean types such as tag attribute disabled selected checked not take effect?