vue中computed和watch的写法

<template>
  <div class="print">
      <div style="color: red">
        <p class="gooe">初始数据--》{{hasOut}}</p>
        <p class="gooe" style="margin-top: 90px">watch监听到的数据----》<span style="color: rebeccapurple">{{watcher}}</span></p>
        <p class="gooe" style="margin-top: 90px">computed监听到的数据----》<span style="color: royalblue">{{gogo}}</span></p>
//这里gogo名字随意,不要加() </div> </div> </template> <script> export default { name: 'print', data () { return { hasOut:0, watcher:0, } }, mounted(){ setTimeout(()=>{ this.testPrint(0) },2000) }, watch: { hasOut(val) { if(val<10000){ this.watcher=val this.testPrint() console.log(this.hasOut,'watch监听') } } }, computed:{
    //写法跟method里面一样。这个名字随意取 gogo:
function(){ return this.hasOut } }, methods:{ testPrint(){ console.log(this.hasOut,'打印出来++++++++++++') setTimeout(()=>{ this.hasOut++ },1000) }, }, } </script>

写法如上下次直接copy

猜你喜欢

转载自www.cnblogs.com/myfirstboke/p/10251181.html