Can't use $refs using variable name instead of direct value

drake035

I'm trying to call a child component's method from my parent component using $refs. It works if I do it the regular way: this.$refs.actualNameOfTheChildComponent.someMethod()

But for my app's needs I have to use a variable name instead of the child component's actual name:

const previousAction = `action${i-1}`
this.$refs.previousAction.showConflict()

In the second line, Vue doesn't understand that I'm not referring to a child component named previousAction, but I'm referring to a child component whose name is the value of the variable previousAction.

What's the right way to do it?

Corylus

Writing .something is just syntactic sugar for ["something"]. Therefore you can use:

const previousAction = `action${i-1}`
this.$refs[previousAction].showConflict()

You can read more about the differences between dot notation and bracket notation here.

There are some important differences between dot and bracket notation:

Dot notation:

  • Property identifies can only be alphanumeric (and _ and $)
  • Property identifiers cannot start with a number.
  • Property identifiers cannot contain variables.
  • OK — obj.prop_1, obj.prop$
  • Not OK — obj.1prop, obj.prop name

Bracket notation:

  • Property identifiers have to be a String or a variable that references a String.
  • It is okay to use variables, spaces, and Strings that start with numbers
  • OK — obj["1prop"], obj["prop name"]

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How can I get Matlab to use the variable value instead of name

in ansible can I use a variable for a module instead of using the module name?

Can you use min() and max() functions to print the variable name instead of the value?

Why can't use let to declare a variable using the same name as one function parameter, even the parameter get set default value?

Why is the object property using variable name as key instead of variable value?

Can't change image source of element ID when using variable name instead of string

Can't use string ("[{"device_name":"iPhone Simulato"...) as an ARRAY ref while "strict refs" in use

Why Can't we use a instead of &a to store value of variable a in C programming lang?

Can't use "go" as variable name in Go

How to use a column's name in an expr() instead of direct value for add date in Pyspark?

How to use min() and max() functions to print the variable name instead of the value?

Using context variable to name a "section" instead of passing a value

Update value using the direct path instead of "an object"

How can I use a variable's value in a variable name in JavaScript?

Why can't I use 'name' as a variable/object name?

Can't use variable in php file instead of css

Assigning a function to the value of a variable instead of the variable name

Can I use "using" instead of "typedef" for a pointer to class member variable?

use a variable instead of variable name with dplyr

jQuery using variable name instead id name

can i use a variable's value as the name of an object in C#

Can I use the value of a variable as a parameter name for a function?

Can't use string as a symbol ref while "strict refs" in use

Can't use string as an ARRAY ref while strict refs in use

Can I use $name instead of ${name}?

@each loop over Map doesn't replace variable with value but prints variable name instead in SASS

Can I access Direct APIs in my app instead of using Docusign SDK and is there any endpoint to get envelopes based on recipient email id or name?

Why can't I use "wide" as variable name?

Can you use a variable name to reference an Object without using eval()