【转】[Vue] vue 的入门示例

在 {{ }} 里面也可以是方法(methods),不过方法需要后面加 ()

更推荐使用 computed 进行计算,不需要后面加 () ,并且相同计算只会执行一次,以下代码段输出的是:

methods
methods
computed

<template>
    <div>
        <div>{{ say() }}</div>
        <div>{{ say() }}</div>
        <div>{{ sayHello }}</div>
        <div>{{ sayHello }}</div>
    </div>
</template>

<script>
    const options ={
        data: function() {
            return {
                name: '张三',
                age: "18"
            }
        },
        methods: {
            say() {
                console.log('methods');
                return this.name + this.age;
            }
        },
        computed:{
            sayHello() {
                console.log('computed');
                return this.name + this.age;
            }
        }
    }

    export default options;
</script>

 

posted on 2025-01-26 14:18  z5337  阅读(10)  评论(0)    收藏  举报