Reputation: 386
I want to get an attribute from an object to be computed, but as far as I'm aware, this is not supported.
I have tried using something like objects.atributes:{ /*codes */ }
but got errors.
I want to achieve something like this:
<template>
<div class="form">
<form>
<div class="form-group">
<label>First Name : </label>
<input type="text" v-model="firstName" name="firstname" class="form-control">
</div>
<div class="form-group">
<label>Last Name : </label>
<input type="text" v-model="lastName" name="lastname" class="form-control">
</div>
<div class="card text-center">
Full name is: {{ formdata.fullname }}
</div>
</form>
</div>
</template>
<script>
export default {
data(){
return {
firstName:'',
lastName:'',
formdata:{
computed:{
fullname:{
get:function(){
return this.firstName + ' ' + this.lastName
}
}
}
},
}
},
/* I have tried this too, and got syntax error
computed:{
formdata.fullname{
get:function(){
return this.firstName + ' ' + this.lastName
}
}
}
*/
}
</script>
I expected the full name to show up, but it didn't.
Upvotes: 1
Views: 746
Reputation: 2777
Computed properties are defined outside of data object. Do it like this:
export default {
data() {
return {
firstName:'',
lastName:'',
}
},
computed: {
fullname: function() {
return this.firstName + ' ' + this.lastName;
}
}
}
<div class="card text-center">
Full name is: {{ fullname }}
</div>
Upvotes: 3