Vue.js Getters and Setters

In this post we will show you Best way to implement Vue get/set methods vs. standard methods , hear for Ultimate Guide to Getters and Setters in VueJS with Download .we will give you demo,Source Code and examples for implement Step By Step Good Luck!.

Getters and setters with VueJS

In this Example,First of all Add or Include External Libs Like as a(jQuery, css etc..), and then create a simple index.php or index.html page.After that crate a simple javascript file like as a index.js or main.js, It is also add your web-application First Header Part to some priority set.After that Include your relevant CSS Class.

In this Example show, simple Vuejs Using computed property setter method and getter method to update data properties

Include External Libs

<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.1.3/vue.js"></script>

index.html

<div id="liveApp">
<child :user-data="realWebsite"></child>
</div>

<template id="child">
<div>
<select v-model="localBase">
<option v-for="localBase in localBases" :value="localBase.id">{{ localBase.name }}</option>
</select>
<p>JSON.stringify(form):</p>
<pre>{{JSON.stringify(form, null, 2)}}</pre>
</div>
</template>

index.js

Vue.component('child', {
template: '#child',
props: ['realWebsite'],
data() {
return {
localBases: [
{name: 'Live24u0', id:0 },
{name: 'Live24u1', id:1 },
{name: 'Live24u2', id:2 },
],
form: { localBases: null }
}
},
computed: {
localBase: {
get: function(){
if(this.realWebsite == undefined) return null;
return this.realWebsite.localBases[0].id;
},
set: function(id) {
var localBase = this.localBases.filter(localBase => localBase.id === id)[0];
Vue.set(this.form.localBases, 0, localBase);
}
}
},
created: function() {
this.form = Object.assign({}, this.realWebsite);
}
});

new Vue({
el: '#liveApp',
data: {
realWebsite: {
localBases: [ {id: 0, name: 'Live24u' } ]
}
}
});

I hope you have Got What is Vue setter and getter methods Example And how it works.


#vue #vuejs 

Vue.js Getters and Setters
1.00 GEEK