美文网首页
2018-09-24

2018-09-24

作者: 99c05f8a86d8 | 来源:发表于2018-09-25 18:53 被阅读0次

    计算属性(computed)用于处理复杂逻辑

    computed:{
    }
    
    

    computed做为vue的选项是固定的
    例子:

    <div id="itany">
        <p>{{mes}}</p>
        <p>{{count}}</p>
    </div>
    <script src="../js/vue.js"></script>
    <script>
        new Vue({
            el:'#itany',
            data:{
                mes:'hello vue'
            },
            computed:{
                count:function(){
                                    //切割       翻转      拼接
                    return this.mes.split(' ').reverse().join('---')
                }
    
            }
        })
    
    </script>
    
    

    输出结果为:
    hello vue
    vue---hello

    练习

    要求:点击button按钮使数字以对应的价格改变

    13921236-6bacda96198b46a1.png
    on v-on:click="num">总和</button>
    <p>{{arr}}</p>
    </div>
    <script src="../js/vue.js"></script>
    <script>
    new Vue({
    el:'#itany',
    data:{
    name:{price:2,count:0},
    names:{price:4,count:0},
    see:true
    },
    methods:{
    num:function(){
    this.name.count++,
    this.names.count++
    }
    },
    computed:{
    arr:function(){
    return this.name.pricethis.name.count+this.names.pricethis.names.count
    }
    }
    })
    </script>

    相关文章

      网友评论

          本文标题:2018-09-24

          本文链接:https://www.haomeiwen.com/subject/noocoftx.html