Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to filter out getters and setters when console logging Vuejs data?

I am writing a vue js app.

When I console log the data from the vue instance I see it with it's getters and setters which are not relevant to me.

var vm = new vue({

  data () { return { testData: { stuff: 'some stuff' } }; },

  methods: {
    log () {
      console.log( this.testData );
    }
  }
})

You can see the above example here.

This is what I get in the console (very dirty):

enter image description here

I can remove the setters before logging but that seems to be overkill for a simple log.

Vue used to have a built in $log method for this purpose, but it has been removed in v2.

Does anyone have any idea how to filter the data from the getters/setters before logging?

like image 452
hitautodestruct Avatar asked Dec 14 '22 02:12

hitautodestruct


2 Answers

One of the following should do the trick: 

log: function(d) {
    console.log(Object.assign({}, this.form));
}

// if you have jQuery
log: function(d) {
    console.log($.extend({}, this.form));
}

// what $log does
log: function(d) {
    console.log(JSON.parse(JSON.stringify(this.form))); 
}

// ES6 Destructuring
log: d => ({ ...d })
like image 94
Mihai Vilcu Avatar answered Dec 17 '22 02:12

Mihai Vilcu


You may do that:

console.log(JSON.parse(JSON.stringify(this.testData)));

new Vue({
   el:"#app",
	data:{
	testData: { 
           stuff: 'some stuff',
           something: 'some thing'
	}
	},
	methods:{
	log(){	    
           console.log(JSON.parse(JSON.stringify(this.testData)));
	}
	}
	})
<script src="https://unpkg.com/vue/dist/vue.js"></script>

<div id="app">
    <button v-on:click="log">log</button>
</div>
like image 27
ABDEL-RHMAN Avatar answered Dec 17 '22 02:12

ABDEL-RHMAN