Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Combining string & variable in element attribute in Vue component template

I have a form where I either have 1 client or 2 clients. I created a component for choosing the count, and another one for displaying client information form are (so if 2 clients, with v-for, 2 forms).

<div id="root">
   <count-section></count-section> // also emits the client count
   <infos
      v-for="(count, index) in total"
      :key="index"
      v-bind:index="count">
   </infos>
</div>

After seting up props, I could catch the count in my component.

In a innerhtml, this one is working working:

<h5>Person {{ count }} Info</h5>

Then I tried to generate an attribute combining string and it gave me error.

<input name="name-client-{{count}}"

name="name-client-{{count}}": Interpolation inside attributes has been removed. Use v-bind or the colon shorthand instead. For example, instead of , use .

What is the proper way of achieving it? Did I thought about the flow the wrong way? I thought of having name-client-1 and name-client-2 together as bunch of all the other fields with same structure and use a for-loop on the backend.

like image 654
senty Avatar asked Aug 02 '17 05:08

senty


People also ask

What is combining strings called?

Concatenation operators join multiple strings into a single string. There are two concatenation operators, + and & . Both carry out the basic concatenation operation, as the following example shows.

How do I combine all strings into one string?

You can concatenate a list of strings into a single string with the string method, join() . Call the join() method from 'String to insert' and pass [List of strings] . If you use an empty string '' , [List of strings] is simply concatenated, and if you use a comma , , it makes a comma-delimited string.

Can you += string?

Use the += operator and the concat() method to append things to Strings. operator, these operators are handy for assembling longer strings from a combination of data objects.

How do I combine double strings?

The “+” operator with a String acts as a concatenation operator. Whenever you add a String value to a double using the “+” operator, both values are concatenated resulting a String object. In-fact adding a double value to String is the easiest way to convert a double value to Strings.


1 Answers

Using ES6 template string :name="`name-client-${count}`".

like image 168
deivuss331 Avatar answered Oct 23 '22 21:10

deivuss331