<script src="https://cdn.jsdelivr.net/npm/vue@2"></script>
<script src="https://cdn.jsdelivr.net/npm/lodash@4.17.21/lodash.min.js"></script>
<div id="vue-search">
<input v-model="searchTerm" @input="search" type="text" placeholder="Search...">
<div v-for="post in posts" :key="post.id">
<h2><a :href="post.link" target="_blank">{{ post.title.rendered }}</a></h2>
<img :src="post.featured_image_url" alt="Post Thumbnail" v-if="post.featured_image_url">
<p v-html="post.excerpt.rendered"></p>
</div>
</div>
body {
font-family: Arial, sans-serif;
}
img{
max-width: 450px;
}
#vue-search {
max-width: 600px;
margin: 0 auto;
}
#vue-search input[type="text"] {
width: 100%;
padding: 10px;
box-sizing: border-box;
margin-bottom: 20px;
}
#vue-search div {
border-bottom: 1px solid #ccc;
padding-bottom: 20px;
margin-bottom: 20px;
}
#vue-search h2 {
font-size: 20px;
margin: 0 0 10px 0;
}
#vue-search a {
text-decoration: none;
color: #333;
}
new Vue({
el: '#vue-search',
data: {
searchTerm: '',
posts: []
},
methods: {
search: _.debounce(function() {
if (this.searchTerm.length < 3) {
this.posts = [];
return;
}
fetch(`https://wreath-ent.co.jp/wp-json/wp/v2/posts?search=${this.searchTerm}`)
.then(response => response.json())
.then(data => {
this.posts = data;
});
}, 500)
}
});
This Pen doesn't use any external CSS resources.
This Pen doesn't use any external JavaScript resources.