Calcular propiedades, métodos y oyentes (3-4)

Propiedades, métodos y oyentes calculados

Cuando elegimos calcular, tenemos un total de 3 formas de lograrlo. Pero somos los primeros en considerar el atributo calculado porque, para el método de métodos, ningún mecanismo de almacenamiento en caché reducirá el rendimiento. Computed y watch tienen un mecanismo de almacenamiento en caché, pero el uso de computed es más abreviado.

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <script src='./vue.js'></script>
</head>
<body>
    <div id="app">
        {
   
   {fullName()}}
        {
   
   {fullName}}
        {
   
   {age}}
    </div>    
    <script>
        var vm = new Vue({
     
     
            el: "#app",
            data: {
     
     
               firstName: "Dell",
               lastName: "Lee",
               fullName: "Dell Lee",
               age: 28
            },
            watch: {
     
     
                firstName: function() {
     
     
                    this.fullName = this.firstName + " " + this.lastName;
                },
                lastName: function() {
     
     
                    this.fullName = this.firstName + " " + this.lastName;
                }
            },
            methods: {
     
     
                fullName: function() {
     
     
                    return this.firstName + " " +this.lastName
                }
            },
            // 计算属性
            computed: {
     
     
                fullName: function() {
     
     
                    return  this.firstName + " " + this.lastName
                }    
            }
        })
    </script>
</body>
</html>

Supongo que te gusta

Origin blog.csdn.net/weixin_45647118/article/details/113830735
Recomendado
Clasificación