3

I want to bind a string containing multiple values to a textfield in vuetify. I wrote the following code but it gives me the properties as string.

<div v-if="textField">
      <v-text-field
      :value="'Our client, {{this.name}}, is {{this.age}} years old.'"
      outline
      readonly
      >
</v-text-field>
</div>

The output is:

Our client, {{this.name}}, is {{this.age}} years old.

Eventhough I want to get this.name and this.age values that are John and 32 something like this:

Our client, John, is 32 years old.

What's the best way to do it?

mha
  • 551
  • 2
  • 11
  • 22

2 Answers2

5

Or use template literals directly in the template as:

<v-text-field
  :value="`Our client, ${name}, is ${age} years old.`"
  outline
  readonly
  >

Also notice Mustaches cannot be used inside HTML attributes. That is, {{}} won't work for attributes.

tony19
  • 125,647
  • 18
  • 229
  • 307
Psidom
  • 209,562
  • 33
  • 339
  • 356
  • I still receive the string `Our client, ${this.name}, is ${this.age} years old.` as the result. – mha Oct 09 '18 at 20:20
  • 2
    Are you using backticks ` around the string? Notice it's not a single quote. – Psidom Oct 09 '18 at 20:22
2

Use a computed field.

{
  computed: {
    textFieldMsg() {
     return `Our client, ${this.name}, is ${this.age} years old.`;
  },
}

HTML

<v-text-field
  :value="textFieldMsg"
  outline
  readonly
  >

Also, depending on what you're trying to do with the text field will determine which solution is best for you. Are the values of this.name and this.age changing depending on other input fields? Is this just static content for the life of the page? Etc...

Stephen Gilboy
  • 5,572
  • 2
  • 30
  • 36