I have two components in a template, the first is a filter and the second makes a request to the API. I would like to know if I can refresh the values of the second component (request) after submit the first (filter)
in main page the request have default value, and if the user use a filter, the request have to change to value that user insert
<template>
<app-filter></app-filter>
<app-request :time="time" :keyword=keyword />
</template>
<script>
export default {
components:{
"app-request": Request,
"app-filter": Filter
},
data() {
return {
keyword: "Hello",
time:"today",
}
}
}
</script>
The filter will change the default value of keyword and time
<template>
<form @submit.prevent="submit">
<input v-model="keyword" class="input" type="text">
<input v-model="time" class="input" type="text">
<button type="submit">send</button>
</form>
</template>
<script>
export default {
data() {
return {
time:"",
keyword: "",
}
},
methods:{
submit(){
//what i do here to change the value in request?
}
},
}
</script>
The request will show the values from API, the request page will receive props from the main page
<template>
<div :time="time"></div>
</template>
<script>
export default {
props:[
'keywords',
'time',
],
create(){
//make a request to api, here is ok
}
}
</script>
How to refresh the main page after submit form in filter component?
window.location.reload()in your submission callback. Assuming it's a fetch/axios call, you would place that in thethencallback of your promise. Having said that, you shouldn't have to refresh the page to update the values of the other component. It would be better to control this behavior via your store with something like VueX. Alternatively, I think that your case is simple enough to where you can just dothis.time = apiCallResponse.timeor whatever your structure looks like. - Evan Bechtol