V
V
Vyacheslav2017-10-12 12:44:30
Vue.js
Vyacheslav, 2017-10-12 12:44:30

How to get data from template in computed method in vue?

<div id="commentsBlock"><vue-comments-item :model="treeComments"></vue-comments-item></div>

Vue.component('item', {
  template: '' +
    '<item>' +
      '<li v-for="comment in model">' +
        '<div>{{comment.users.username}}<br />{{comment.text}}</div>' +
        '<ul :model="comment" v-if="replies">' +
          '<vue-comments-item v-for="model in comment.replies" :model="model"></vue-comments-item>' +
        '</ul>' +
      '</li>' +
    '</item>' +
  '',
  props: {
    model: Object
  },
  computed: {
    replies: function () {
      console.log(JSON.stringify(this.model, null, 4));
    }
  },
})

how in computed to access the comment iteration of the model object?
this.model displays the entire model object, and I need it to display the current object v-for
tried to override :mode="comment" - the result is zero
, tell someone pliz)) spent all day killing this damn tree

Answer the question

In order to leave comments, you need to log in

2 answer(s)
E
Evgeny Kulakov, 2017-10-12
@nskarl

Make the method simple:

methods: {
replies(v){
 // some code
}
}

In template:
<ul :model="comment" v-if="replies(comment)">

V
Vadim Milevsky, 2017-10-12
@3gsxarakiri

I could be wrong, so I'm not trying to be the answer. But computed is not a method, but a variable that is pre-calculated.

computed: {
    replies: function () {
      return 2+2;
    }
  },

<p>{{ replies }}</p>// 4

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question