Vue Pairing Example - Craig Riggins
by Sarah Godoshian
HTML
<div id="app">
<h2>Display Posts for User</h2>
<select v-on:change="fetchUserPosts(selectedUserId)" v-model="selectedUserId">
<option :value="null">Select a User</option>
<option v-for="user in users" :value="user.id">
{{ user.name }}
</option>
</select>
<
</div>
CSS
body {
background: #20262E;
padding: 20px;
font-family: Helvetica;
}
#app {
background: #fff;
border-radius: 4px;
padding: 20px;
transition: all 0.2s;
}
h2 {
font-weight: bold;
margin-bottom: 15px;
}
Vue
new Vue({
el: "#app",
data() {
return {
users: null,
selectedUserId: null,
userPosts: null,
}
},
mounted() {
this.fetchUsers();
},
methods: {
fetchUsers() {
return fetch('https://jsonplaceholder.typicode.com/users')
.then((response) => {
if (response.ok) {
return response.json();
}
})
.then((json) => {
this.users = json;
})
},
fetchUserPosts(uid) {
// TODO: https://jsonplaceholder.typicode.com/posts?userId=1
fetch(' https://jsonplaceholder.typicode.com/posts?userId='+uid)
.then((response) => {
if (response.ok) {
return response.json();
}
})
.then((json) => {
this.userPosts = json;
})
}
}
})