I have a parent component ChangeInfo, inside it is a child component ShowWorkInfo. Inside the ShowWorkInfo component, I have several input forms to update the job information. I created a variable work_info which is an object and then used the v-model for the fields inside. But I don't know how to make the parent component get data from the child component. I don't have any button inside the child component, I'll try to handle the data from WorkInfo with a button in the parent. This is my code. Should I write directly into ChangeInfo without splitting it into a child component
ChangeInfo (parent component)
export default class ChangeInfo extends Vue {
public isUpdated: boolean = false
updateWorkInfo(workInfo: any) {
if (
workInfo.company == '' ||
workInfo.department == '' ||
workInfo.position == '' ||
workInfo.postcode == '' ||
workInfo.prefectures == '' ||
workInfo.municipality == '' ||
workInfo.address == '' ||
workInfo.building == '' ||
workInfo.phone_number == '' ||
workInfo.url == ''
) {
alert('完全な情報を入力してください')
this.isUpdated = false
} else {
axios
.put('https://609b82962b549f00176e394f.mockapi.io/work_info/1', {
status: workInfo.status,
company: workInfo.company,
department: workInfo.department,
position: workInfo.position,
postcode: workInfo.postcode,
prefectures: workInfo.prefectures,
municipality: workInfo.municipality,
address: workInfo.address,
building: workInfo.building,
phone_number: workInfo.phone_number,
url: workInfo.url
})
.then(response => {
workInfo = response.data
console.log(workInfo)
InformationModule.CHANGE_WORK_INFO(workInfo)
})
.catch(error => console.log(error))
this.isUpdated = false
workInfo.status = false
workInfo.company = ''
workInfo.department = ''
workInfo.position = ''
workInfo.postcode = ''
workInfo.prefectures = ''
workInfo.municipality = ''
workInfo.address = ''
workInfo.building = ''
workInfo.phone_number = ''
workInfo.url = ''
}
}
updatePersonalInfo(personalInfo: any) {
if (
personalInfo.nearest_station == '' ||
personalInfo.postcode == '' ||
personalInfo.prefectures == '' ||
personalInfo.municipality == '' ||
personalInfo.address == '' ||
personalInfo.building == '' ||
personalInfo.phone_number == '' ||
personalInfo.url == ''
) {
alert('完全な情報を入力してください')
this.isUpdated = false
} else {
axios
.put('https://609b82962b549f00176e394f.mockapi.io/personal_info/1', {
gender: personalInfo.gender,
nearest_station: personalInfo.nearest_station,
postcode: personalInfo.postcode,
prefectures: personalInfo.prefectures,
municipality: personalInfo.municipality,
address: personalInfo.address,
building: personalInfo.building,
phone_number: personalInfo.phone_number,
url: personalInfo.url
})
.then(response => {
personalInfo = response.data
console.log(personalInfo)
InformationModule.CHANGE_PERSONAL_INFO(personalInfo)
})
.catch(error => console.log(error))
this.isUpdated = false
personalInfo.gender = false
personalInfo.nearest_station
personalInfo.postcode = ''
personalInfo.prefectures = ''
personalInfo.municipality = ''
personalInfo.address = ''
personalInfo.building = ''
personalInfo.phone_number = ''
personalInfo.url = ''
}
}
triggerSubmit() {
this.isUpdated = true
}
I call two functions like this
<template>
<div class="d-block">
<ShowProfile />
<ShowWorkInfo :isUpdated="isUpdated" @update-work-info="updateWorkInfo" />
<ShowPersonalInfo
:isUpdated="isUpdated"
@update-personal-info="updatePersonalInfo"
/>
<div class="w--27 mw-100 mx-auto my-9">
<button
@click="triggerSubmit"
v-b-modal="'update-success'"
class="btn btn-primary w-100"
>
{{ $t('common.btn.btn_update') }}
</button>
</div>
<ModalUpdateSuccess />
</div>
</template>