0

Sorry for my begginner question, but I'm stucked since yesterday on a basic problem which i cannot figure out the solution.

I want to fill my variable logs with some json object and render it in an array.

Here is my html :

<tbody>

  <tr v-for="log in logs">
    <td>{{log.incident}}</td>
    <td>{{log.request}}</td>
    <td>{{log.requested}}</td>
    <td>{{log.message}}</td>               
  </tr>            

</tbody>

and here is my JS

let vm = new Vue({
    el: '#container',
    components: {
        'table-header': tableHeader,
    },
    data: {
        logs: []
    },
    methods: {
        getLogs() {
            addon.port.on('log', function (data) {
               console.log(data);
               this.$add('logs',data)
            });
        }
    },
    ready:{
        function(){this.getLogs()}
    }

});

vm.getLogs();
  1. The ready does not seems to work. Why?
  2. The this.logs is undefined? Why?
  3. It complains that the "this.$add is not a function"
  4. In the html the logs are never populated in the loop. why?
4
  • Check what vue version you have. In v2 they replaced 'ready' with 'mounted' Commented Nov 3, 2016 at 11:21
  • Please let us know, what VueJS version you are using. Commented Nov 3, 2016 at 11:30
  • I use the version 2.0.3. Okey it works now with the mount Commented Nov 3, 2016 at 12:00
  • here is the code inside jsfiddle.net/#&togetherjs=gIQLorBo7t Commented Nov 3, 2016 at 13:08

3 Answers 3

1
  1. Your ready should not be an object, it should be a function

2&3&4. Inside an anonymous function, this refers to something else. Use an arrow function to keep the scope of this, or store a reference to this

let vm = new Vue({
     el: '#container',
     components: {
         'table-header': tableHeader,
     },
     data: {
         logs: []
     },
     methods: {
         getLogs() {
             addon.port.on('log', (data) => {
                console.log(data);
                this.$add('logs',data)
             });
         }
     },
     ready() {
         this.getLogs()
     }

     });

//OR do this
getLogs() {
  var that = this;
             addon.port.on('log', function(data) {
                console.log(data);
                that.$add('logs',data)
             });
         }

Sign up to request clarification or add additional context in comments.

Comments

0

You can also use mounted, something like:

mounted () {
    this.getLogs()    
}

Documentation.


data must be a function for a Vue component, but for a Vue instance it should be fine..

So for a Vue component, it should have data like following:

data: function () {
  return {
    logs : []
  }
}

2 Comments

data should not be function if it's not part of component .If it's part of components It should be function that return object.
@thiebautfischer yes.
0

I don't know if your example is valid and I can't test it at the moment but I am used to the syntax like so:

ready: function(){
    console.log('ready);
}

Same goes for your methods. Maybe check the documentation and apply the syntax like given there to begin with.

https://vuejs.org/guide/index.html#Getting-Started

3 Comments

yes I tried this way but it does not work, it does not print ready
I did not find proper documentation about ready, but as far as I understood it is running the functions inside ready once the object is created?
It would help a lot if you could make a small example in JSfiddle.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.