1. 程式人生 > >Vue開發——watch監聽陣列、物件、變數

Vue開發——watch監聽陣列、物件、變數

1.普通的watchdata() {    return {        frontPoints: 0        }},watch: {    frontPoints(newValue, oldValue) {        console.log(newValue)    }}2.陣列的watch:深拷貝data() {    return {        winChips: new Array(11).fill(0)       }},watch: {  winChips: {    handler(newValue, oldValue) {      for (let i = 0; i < newValue.length; i++) {
        if (oldValue[i] != newValue[i]) {          console.log(newValue)        }      }    },    deep: true  }}3.物件的watchdata() {  return {    bet: {      pokerState: 53,      pokerHistory: 'local'    }       }},watch: {  bet: {    handler(newValue, oldValue) {      console.log(newValue)    },    deep: true
  }}4.物件的具體屬性的watch:data() {  return {    bet: {      pokerState: 53,      pokerHistory: 'local'    }       }},computed: {  pokerHistory() {    return this.bet.pokerHistory  }},watch: {  pokerHistory(newValue, oldValue) {    console.log(newValue)  }}