I’m building an app that uses Stackoverflow’s API to get the last question of a certain tag & ouput it on my app. I have been able to get the last question on the specific tag, but now I want that automatically detect changes in the API Endpoint & fetch the last question posted without refreshing the browser or calling the function. I have tried with Computed Properties & Watchers, but I haven’t got it to work. I might be doing it wrong or my solution is not the right approach.
This is my code snippet:
<template>
<div class="question container-fluid">
<h1 class="my-3"
>
Last "<span class="question_tag">{{ tag }}</span>" Question Details
</h1>
<b-form
class="my-4"
@submit.prevent="updateTag"
>
<b-form-input
class="w-auto"
placeholder="Enter a stackoverflow tag"
type="text"
v-model="newTag"
>
</b-form-input>
</b-form>
<div class="question_title my-2">
<strong>Title: </strong> <span>{{ details.title }}</span>
</div>
<div class="question_status my-2">
<strong>Question answered: </strong><span>{{ details.is_answered }}</span>
</div>
<div class="question_views my-2">
<strong>Views: </strong><span>{{ details.view_count }}</span>
</div>
<div class="question_answers my-2">
<strong>Answers Count: </strong><span>{{ details.answer_count }}</span>
</div>
<div class="question_link my-2">
<strong>Check question: </strong>
<a :href="details.link"
:title="details.link"
rel="noopener"
target="_blank"
>
Here
</a>
</div>
</div>
</template>
<script>
import axios from 'axios'
export default {
name: 'QuestionInfo',
data(){
return {
details: '',
tag: `phaser-framework`,
newTag: ''
}
},
mounted() {
this.getTag()
},
methods: {
getTag(){
axios.get(`https://api.stackexchange.com//2.2/questions?order=desc&sort=creation&tagged=${this.tag}&site=stackoverflow`).then(response => {
this.details = response.data.items[0]
// console.log an error if get() method is unsuccessful
}).catch(err => {
console.log(err)
})
},
updateTag() {
this.tag = this.newTag
this.getTag();
},
},
}
</script>
EDIT:
I would like to find a Client Side solution... preferably a Vue Solution if there is any. I would like to avoid as much as possible a Server Side Solution for this feature.