计算属性的基本操作和复杂操作

简介: 计算属性的基本操作和复杂操作

计算属性的setter和getter操作

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
    <div id="app">{{fullname}}</div>
    <script src="../vue.js"></script>
    <script>
        const app = new Vue({
            el: '#app',
            data: {
                lastname: "SF",
                firstname: "L"
            },
            computed: {
                fullname: {
                    // 计算属性一般只有get属性只读属性
                    set: function(newvalue){
                        const name = newvalue.split(" ")
                        this.firstname = name[0]
                        this.lastname = name[1]
                    },
                    get: function(){
                        return this.firstname + " " + this.lastname
                    }
                }
            }
        })
    </script>
</body>
</html>

计算属性的基本操作:

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
    <div id="app">
        <h1>{{getfullName1()}}</h1>
        <h1>{{getfullName2()}}</h1>
        <h1>{{fullName}}</h1>
    </div>
    <script src="../vue.js"></script>
    <script>
        const app = new Vue({
            el: '#app',
            data: {
                lastname: "SF",
                firstname: "L"
            },
            computed: {
                fullName: function(){
                    return this.firstname + " " + this.lastname
                }
            },
            methods: {
                getfullName1: function(){
                    return this.firstname + " " + this.lastname
                },
                getfullName2(){
                    return this.firstname + " " + this.lastname
                }
            },
        })
    </script>
</body>
</html>

计算属性的复杂操作:

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
    <div id="app">总价格:{{totalPrice}}</div>
    <script src="../vue.js"></script>
    <script>
        const app = new Vue({
            el: '#app',
            data: {
                books: [
                    {id: 1, name: "Unix编译艺术", price: 110},
                    {id: 2, name: "代码大全", price: 105},
                    {id: 3, name: "深入理解计算机原理", price: 85},
                    {id: 4, name: "现代操作系统", price: 90}
                ]
            },
            computed: {
                totalPrice: function(){
                    let res = 0
                    for(let i = 0; i < this.books.length; i++){
                        res += this.books[i].price
                    }
                    return res
                }
            }

        })
    </script>
</body>
</html>
目录
相关文章
|
5月前
|
存储 数据可视化 C语言
C 语言数组教程:定义、访问、修改、循环遍历及多维数组解析
数组用于将多个值存储在单个变量中,而不是为每个值声明单独的变量。 要创建数组,请定义数据类型(例如 int)并指定数组名称,后面跟着方括号 []。 要将值插入其中,请使用逗号分隔的列表,并在花括号内使用
1069 0
|
2月前
|
容器
aside元素的注意事项
【8月更文挑战第31天】aside元素的注意事项。
12 1
|
3月前
|
Java Serverless Docker
函数计算操作报错合集之创建 bodySyream时报错,是什么原因
Serverless 应用引擎(SAE)是阿里云提供的Serverless PaaS平台,支持Spring Cloud、Dubbo、HSF等主流微服务框架,简化应用的部署、运维和弹性伸缩。在使用SAE过程中,可能会遇到各种操作报错。以下是一些常见的报错情况及其可能的原因和解决方法。
|
5月前
|
存储 算法 Python
集合在编程中的应用与实例
集合在编程中的应用与实例
91 1
|
5月前
|
存储 索引 Python
数组的操作方法
数组的操作方法
81 1
|
存储 算法 Serverless
【100天精通python】Day7:数据结构_列表 List的创建、删除、访问、遍历、统计、排序、推导等使用
【100天精通python】Day7:数据结构_列表 List的创建、删除、访问、遍历、统计、排序、推导等使用
127 0
|
异构计算 并行计算 算法框架/工具
假设Xdata放在GPU上面,逐一遍历它的元素,假设元素变量为x,那么x也在GPU上面吗?
是的,如果Xdata放在GPU上面,并逐一遍历它的元素时,每个元素x也会在GPU上。 当你迭代一个张量时,逐一访问它的元素,这些元素是作为新的张量对象返回的。如果原始张量在GPU上,则返回的新张量也将在GPU上。因此,在迭代过程中,无论是通过循环、列表解析还是其他方法,所有张量都将保持在GPU上
145 0
|
存储 编译器 程序员
C++数据定义及相关操作
C++数据定义及相关操作
134 0
C++数据定义及相关操作
数据结构105-插入和修改操作封装代码
数据结构105-插入和修改操作封装代码
76 0
|
NoSQL Redis 开发者
通用指令-key 基本操作 | 学习笔记
快速学习通用指令-key 基本操作
通用指令-key 基本操作 | 学习笔记
下一篇
无影云桌面