Are methods in Vue reactive?

前端 未结 3 1838
后悔当初
后悔当初 2021-01-02 01:38

I\'ve been using Vue for a while, and my experience has always been a method will recompute if its underlying reactive data is updated. I\'ve encountered conflicting informa

3条回答
  •  -上瘾入骨i
    2021-01-02 02:06

    No, methods are not reactive. Only data can be reactive in Vue.

    BUT its is important understand how Vue works...

    1. Vue will take your template, compiles it into render function and runs the render function
    2. While the render function is running, Vue monitors all data() members (it does this all the time, not only during 1st render). If any of the data is accessed during render, Vue knows that content of this data member influences the result of rendering.
    3. Vue use the knowledge gathered in step 2. Whenever data member "touched" during rendering changes, it knows re-render should happen and do the thing...

    It doesn't matter if you reference the data member directly, use it in computed or in a method. If the data is "touched" during rendering, the change of the data will trigger re-render in the future...

提交回复
热议问题