admin管理员组

文章数量:1794759

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

计算属性的setter和getter操作

代码语言:javascript代码运行次数:0运行复制
<!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>

计算属性的基本操作:

代码语言:javascript代码运行次数:0运行复制
<!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>

计算属性的复杂操作:

代码语言:javascript代码运行次数:0运行复制
<!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>
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。 原始发表:2023-01-09,如有侵权请联系 cloudcommunity@tencent 删除htmlappscript编译原理

本文标签: 计算属性的基本操作和复杂操作