我加入了一个清零按钮,但是清零的时候计算属性还是执行并且count+1了,只能在再加一个count--才能解决

来源:2-5 Vue中的计算属性和侦听器

慕虎3255728

2019-08-06 15:05

<div id="root">

姓:<input v-model="firstName" />

名:<input v-model="lastName" />

<div>{{fullName}}</div>

<div>{{count}}</div>

<button @click="firstName='',lastName='',count =0 ,count --" v-model="clear">清除</button>

</div>

<script>

new Vue({

el:"#root",

data:{

firstName:'',

lastName:'',

count:0 ,

clear:''

},

computed:{

fullName: function(){

return this.firstName+' '+this.lastName

}

},

watch:{

fullName:function(){

this.count ++

},

},

})

</script>


写回答 关注

3回答

  • 慕码人0226809
    2020-04-15 15:17:02

    不能直接这样吗

    <button type="reset" value="重置按钮"></button>


  • akakidz
    2019-08-08 15:02:24

    我加了两个变量,zero判断是否通过input修改传值,firstC判断是否第一次点击重置按钮

    watch: {

    num:function(){

    if(!this.zero)

    {this.count++,firstC=true}

    else{

    this.zero = false

    }

    }

    },


    methods: {

    reset:function(){

    if(firstC){

    this.num='';

    this.zero = true;

    this.count = 0;

    firstC = false;

    }else{

    alert("点一次就行了!")

    }

    }

    },


  • akakidz
    2019-08-07 00:37:43

    count--应该是最好的解决办法了...

    慕虎3255... 回复akakid...

    学到了,谢谢?

    2019-08-10 09:58:02

    共 3 条回复 >

vue2.5入门

快速理解Vue编程理念上手Vue2.0开发。

146231 学习 · 657 问题

查看课程

相似问题