Preface
In Vue,this.$refs
Used to access the internal component or on the DOM elementref
An element or child component instance referenced by the attribute. If you encounterthis.$refs
Get asundefined
The situation is usually caused by the following reasons:
reason
Incorrect access time: If you try to ask before the component rendering (or the child component mounting) is completed
this.$refs
, it will beundefined
. Because at this time the DOM element or child component may not have been mounted or rendered yet.Conditional Rendering:If yours
ref
The element or component is conditionally rendered (for example, it is usedv-if
), and under the current conditions, the element or component is not rendered, thenthis.$refs
Will beundefined
。Used in method but not properly bound to Vue instance: If you use it in one method
this.$refs
, but this method is not a method of a Vue instance (such as calling directly in a template expression), or a method'sthis
The context is changed (for example, using arrow functions as event handlers), thenthis
May not point to the Vue instance, resulting inthis.$refs
forundefined
。
Solution
-
Make sure the access time is correct:
- In Vue's life cycle hook,
mounted
After the hook, you can ensure that the DOM element has been mounted and is accessed.this.$refs
A good time. - If you are in the component
created
Access in hookthis.$refs
, it will beundefined
, because the template of the component has not been mounted to the DOM at this time.
- In Vue's life cycle hook,
-
Check conditional rendering:
- Make sure to access it when you try
this.$refs
When the relevant elements or components are indeed rendered. - If used
v-if
, try to use it insteadv-show
(if applicable), or make sure to accessthis.$refs
Before, the conditions were indeedtrue
。
- Make sure to access it when you try
-
make sure
this
Point to the correct:- In the Vue component method, make sure to use
function
Declare methods, not arrow functions, unless you have special reasons to changethis
Pointing. - If you access it directly in the template expression
this.$refs
, consider moving it into the method of the Vue instance and calling the method in the template.
- In the Vue component method, make sure to use
-
Use nextTick:
- If you need to access immediately after the data changes
this.$refs
, but the DOM update has not been completed yet, you can use()
method. This method delays the callback until the next DOM update loop is over, when both the DOM element and child component instance have been updated.
- If you need to access immediately after the data changes
this.$nextTick(() => { // DOM has been updated and this.$refs is now safe to use (this.$); });
This.$refs. attribute is undefined reason
1. For the reason why this.$refs. property is undefined, it should be understood as follows:
In Vue, this.$refs is an object used to access references to a component or DOM element. It contains references declared in a template using the ref attributes, through which properties and methods of components or DOM elements can be accessed.
When declaring a reference in a component using the ref attribute, for example:
<el-input ref="test" type="textarea"/>, you can get instances of child components through this.$. And if you declare a reference on a normal DOM element using the ref attribute in a template, for example: <div ref="test"></div>, then this.$ will point directly to the DOM element itself, not the component instance. In this case, you can directly use this.$ to access the properties of the DOM element, such as offsetHeight, clientHeight, etc.
Summarize:The reference in this.$refs depends on how you use the ref property in the template. If you use ref on the component, the reference in this.$refs will point to the component instance. If you cannot use the attributes of the DOM element directly, it will return undefined. On the component instance, there is a $el attribute pointing to the real DOM element, so you can use the this.$refs.$el. attribute to get the attributes of the DOM element; if you use ref on a normal DOM element, the reference in this.$refs will point to the DOM element itself.
Summarize
This is the article about the reasons and solutions for obtaining this.$refs as undefined in Vue. For more related content to obtaining this.$refs as undefined, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!