Transform array vue into Json

1

I am trying to pass in an textarea an array of Vue.js, and would like to turn it into json .

What would be the best way to do this?

    new Vue({
    el:"#app",

    data : {
      nome_da_variavel_array: '[{variavel : 'valor'},{variavel : 'valor'},{variavel : 'valor'}]'
    }
}); 
<textarea>{{nome_da_variavel_array}}</textarea>
    
asked by anonymous 06.09.2018 / 20:59

1 answer

5

A good approach would be to create a computed property that always returns an object of data converted to JSON.

var app = new Vue({
  el: '#app',
  data: {
    json: {
      teste: "Valor",
      um_array: [0, 1, 2, 99]
    }
  },
  computed: {
    json_string: function() {
      return JSON.stringify(this.json);
    }
  }
});
<script src="https://cdn.jsdelivr.net/npm/vue"></script><divid="app">
    <textarea>{{ json_string }}</textarea>
</div>
    
06.09.2018 / 21:22