How to watch prop even if it does not change?

前端 未结 1 1656
死守一世寂寞
死守一世寂寞 2021-01-29 02:27

I have watcher with the same name as a prop. The prop is dynamically set in a parent component on some event. I need something that triggers some function in the child component

相关标签:
1条回答
  • 2021-01-29 03:11

    You could use immediate option :

    watch:{
      yourProp:{
       handler(newval,oldVal){
    
       },
       immediate:true,
       deep:true //if the prop is an object or an array
    
     }
    
    }
    
    

    EDIT

    since the old value is the same as the new one, you could try this workaround, by defining the prop as an object by adding a field that always changes :

    data()
    {
        return {
            replyClicked : {
              changeCount:0,
              id:null
             }
        }
    },
    methods :
    {
        onReply(id)
        {
            this.replyClicked ={id, changeCount:this.replyClicked.changeCount+1}
        }
    }
    

    child component :

    watch: {
        replyClicked:
        {
            handler(newVal, oldVal)
            {
                console.log(newVal, oldVal);
            },
            immediate:true,
            deep:true
        }
    },
    
    0 讨论(0)
提交回复
热议问题