I have a Vue app in a single file component which allows the user to lookup a github username and see the fullname, login, and country the user is from.
Here is my component:
<template>
<div id="app">
<form @submit.prevent="search">
<input v-model="username" />
</form>
<p v-if="data">
{{ data.name }} ({{ data.login }})
is from
{{ data.location }}!
</p>
</div>
</template>
<script>
import Vue from 'vue'
import axios from 'axios'
import VueAxios from 'vue-axios'
Vue.use(VueAxios, axios)
export default {
data() {
return {
username: '',
data: []
}
},
methods: {
search() {
const api = `https://api.github.com/users/${this.username}`
Vue.axios.get(api).then((response) => {
this.data = response.data
}).catch(error => {
console.log(error)
})
}
}
}
</script>
My issue is: I cannot find a way to handle the situation in which the user enters a user that doesn't exist in github. With the catch block of the axios promise, I expect the error to be logged to console, but it isn't. The reason I want to be able to handle this occasion is that I don't want the placeholder text () is from ! when either nothing has been searched, or an invalid search has been made.
I tried to use data.length instead of just data for the v-if check, but it seems like my component doesn't 'react' when this is the case; I can see the data change in Vue dev tools but not in the component. What could be happening here?
Here is a webpackbin demo of the app: http://www.webpackbin.com/VJlfcrGLM