I have a fetch request in Home.vue file and I want to use the same data to another vue file (AllJobs.vue) with the same component(props). How I can achieve this without make a new fetch request in AllJobs.vue? I heard that I can use Pinia, but for a small project like this I wonder if it is a simpler solution?
Home.vue
<div class="cards-container">
<div v-for="job in jobs.reverse().slice(0, 5)" :key="job.id" class="">
<JobComponent :position="job.position" :department="job.department" :location="job.location"/>
</div>
</div>
</template>
<script>
import JobComponent from '../components/JobComponent.vue'
export default {
name: 'Home',
components: { JobComponent },
data() {
return {
jobs: [],
}
},
mounted() {
fetch("http://localhost:3000/jobs")
.then(res => res.json())
.then(data => this.jobs = data)
.catch(err => console.error(err.message))
}
}
</script>
AllJobs.vue
<div class="cards-container">
<JobComponent />
</div>
</template>
<script>
import JobComponent from '../../components/JobComponent.vue'
export default {
name: "AllJobs",
components: { JobComponent },
}
</script>
JobComponent.vue
<div class="card">
<div class="position">{{ position }}</div>
<div class="department">{{ department }}</div>
<div class="location">
<span class="material-symbols-outlined">
location_on
</span>
{{ location }}
</div>
<span class="material-symbols-outlined right-arrow">
arrow_right_alt
</span>
</div>
</template>
<script>
export default {
props: ['position', 'department', 'location'],
}
</script>