Is There A Way To Shorten Defining V-model Data, Vue.js And Laravel
On my edit page of CRUD project, I have a code that fills the form with values of which record is being edited. I use v-model to define HTML inputs, but the code seems too long. I
Solution 1:
You can change your model of data adding a new layer. For example:
data() {
return {
currentStudent: {
first_name: '',
last_name: '',
student_number: '',
phone_number: '',
email: '',
birth_date: '',
school_name: '',
}
}
},
Then in created
you can use simple
created() {
this.currentStudent = this.student;
this.currentStudent.birth_date = moment(this.student.birth_date).format('YYYY-MM-DD');
},
And in all component replace names by names with currentStudne
eg in v-models
:
first_name -> currentStudne.first_name
You can also read about Vue.$set
Solution 2:
You can use the object studentData
, it is working well with v-model
.
First, you pass the props like that :
<student-edit-component :student="student">
(no need to use the ${{}}
).
Then in the component `StudentEditComponent', you can use :
props: {
student: {
type:Object,
required:true,
default :()=> {},
}
}
You should use the type, required and default properties, it is a good practice. Then
data () {
return {
studentForm: {},
};
},
created() {
this.studentForm = this.student;
}
In the template, you can after that use v-model="studentForm.first_name"
Post a Comment for "Is There A Way To Shorten Defining V-model Data, Vue.js And Laravel"