DevonDahon
DevonDahon

Reputation: 8350

VueJS: How to call a variable from string instead of direct variable name in a template?

How to call a variable set in <script> from the template using its string name?

For instance, in the example below, I'd like to call the foo variable using table[0]:

<template>
  <div>
    {{ table[0] }}
    {{ table[1] }}
  </div>
</template>

<script>
export default {
  data() {
    return {
      foo: 'Hello World!',
      bar: '大家好!',
      table: ['foo', 'bar']
    }
  }
}
</script>

Upvotes: 1

Views: 374

Answers (1)

Lawrence Cherone
Lawrence Cherone

Reputation: 46602

By using $data, like {{ $data[table[0]] }}

Upvotes: 5

Related Questions