Subscribe on changes!

how to watch props change by using custom directive like component

avatar
Sep 30th 2021

Version

3.2.19

Steps to reproduce

<div v-custom-dir="{ a }"></div>

app.directive("custom-dir", {
  // support `props` 
  props: ["a"],
  // add `props` arguments when mounted or provide setup hook
  mounted(props) {
     // watch props change
     watch(() => props.a, console.log)
  }
})

What is expected?

provide any channel to watch props change in directive

What is actually happening?

nothing can do to watch reactive props


l know using component can resolve it,but l need to modify DOM,it will append an extra node,and change it's behavior when props was changed

now l depend on update hook,it will emit when parent updated

app.directive("custom-dir", {
   update(el, { value }){
     if(value.a === "b"){
         // do something
       }
  }
})

Is there any other way?

avatar
Sep 30th 2021

You are not watching a, you are watching an function that returns props.a. Try

watch(props.a, console.log)

instead.

Also this place is not for asking questions. Try https://stackoverflow.com/questions/tagged/vue.js instead.

avatar
Sep 30th 2021

Using updated should be fine.


Remember to use the forum or the Discord chat to ask questions!