uni-app计算属性(computed)、方法(methods)、监听(watch)

例如:

<template>
    <view>
        {{fullText}}
        <button type="primary" @click="click">change</button>
        {{name}}
    </view>
</template>

<script>
    export default {
        data() {
            return {
                firstText: 'hello',
                lastText: 'world',
                fullText:'hello world',
                name: 'qq'
            }
        },
        methods: {
            click() {
                this.firstText = 'tt'
            },
            // fullText() {
            //  console.log('方法')
            //  return this.firstText + ' ' + this.lastText;
            // }
        },
        watch:{
            firstText() {
                console.log('监听')
                this.fullText = this.firstText + ' ' + this.lastText;
            },
            lastText() {
                console.log('监听')
                this.fullText = this.firstText + ' ' + this.lastText;
            }
        }
        // computed:{
        //  fullText(){
        //      console.log('计算属性')
        //      return this.firstText + ' ' + this.lastText;
        //  }
        // }
    }
</script>

<style>

</style>

1、对于计算属性来说,当计算属性中的值(firstText、lastText)发生改变的时候,则计算属性中的相关方法(fullText)将被触发
2、对于监听来说,当变量值(firstText、lastText)发生改变的时候,监听中变量值的监听方法(firstText、lastText)将被触发
3、对于方法来说,当页面发生重新渲染的时候,方法将被重新执行

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容