How do I access $refs
inside computed? It's always undefined the first time the computed property is run.
相关问题
- Axios OPTIONS instead of POST Request. Express Res
- Vue.js - set slot content programmatically
- Getting Uncaught TypeError: …default is not a cons
- Vue.js computed property loses its reactivity when
- How to remove the Vuetify append-icon from the seq
相关文章
- Best way to dynamically change theme of my Vue.js
- vue-router how to persist navbar?
- How to Properly Use Vue Router beforeRouteEnter or
- Difference between nameFunction() {} and nameFunct
- Vue - best way to reuse methods
- setTimeout() not working called from vueJS method
- How to unbind an array copy in Vue.js
- Vue Cli 3 Local fonts not loading
For others users like me that need just pass some data to prop, I used
data
instead ofcomputed
I just came with this same problem and realized that this is the type of situation that computed properties will not work.
According to the current documentation (https://vuejs.org/v2/guide/computed.html):
"[...]Instead of a computed property, we can define the same function as a method. For the end result, the two approaches are indeed exactly the same. However, the difference is that computed properties are cached based on their reactive dependencies. A computed property will only re-evaluate when some of its reactive dependencies have changed"
So, what (probably) happen in these situations is that finishing the mounted lifecycle of the component and setting the refs doesn't count as a reactive change on the dependencies of the computed property.
For example, in my case I have a button that need to be disabled when there is no selected row in my ref table. So, this code will not work:
What you can do is replace the computed property to a method, and that should work properly:
Going to answer my own question here, I couldn't find a satisfactory answer anywhere else. Sometimes you just need access to a dom element to make some calculations. Hopefully this is helpful to others.
I had to trick Vue to update the computed property once the component was mounted.
If you need the
$refs
after anv-if
you could use theupdated()
hook.Use property binding if you want. :disabled prop is reactive in this case
But to check two fields i found no other way as dummy method:
I think it is important to quote the Vue js guide:
It is therefore not something you're supposed to do, although you can always hack your way around it.