0

I have a code like below.

data():{
   coor: [
          { "name": '',
            "shoes": '' },
          { "name": '',
            "shoes": '' }       
         ]

watch:{
   coor: {
       handler(){
          console.log('changed')
       }
       ,deep: true
   }
}

In this case if whichever value in coor is changed, watch works.
But my goal is I want to make the case divided by two like below.

watch:{
       coor.name: {
           handler(){
              console.log('The name is changed')
           }
           ,deep: true
       },
       coor.shoes: {
           handler(){
              console.log('The shoes are changed')
           }
           ,deep: true
       }
    }

But it doens't work well. How can I solve this problem? Thank you so much for reading.

1
  • your coor.name and coor.shoes is undefined... it should be coor[0].name... but that is hardcoded solution and not good... Commented Sep 22, 2019 at 12:40

1 Answer 1

2

Create a computed property of the items you want to watch, and watch that instead.

computed {
  filteredAny(){
    return this.coor;  
    // you can also use map here and combine values to create a key.
    // return this.coor.map(it=>it.shoes+':'+it.name)
  },
  filteredShoes(){
    return this.coor.map(it=>it.shoes);
  },
  filteredName(){
    return this.coor.map(it=>it.name);
  }

}

watch {
  // deep not needed.
  filteredShoes(){
    console.log('The shoes are changed');
  }

}
Sign up to request clarification or add additional context in comments.

3 Comments

Could you write the code exactly? I dont't understand well by just text.
I have a question. Your code works when I typed, "return this.coor.filter(it=>it.name === 'name');" . Than is it has to add "it.name" not "it". But How can I modify for the any case?
I am going to update the solution based on your comment. Let me know if it works.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.