Skip to content

Instantly share code, notes, and snippets.

@kmaher9
Created September 23, 2018 22:31
Show Gist options
  • Save kmaher9/c142ddfc7c763782d266fe03956225f8 to your computer and use it in GitHub Desktop.
Save kmaher9/c142ddfc7c763782d266fe03956225f8 to your computer and use it in GitHub Desktop.
<template>
<div>
<div>
<h1 style="margin-top: 5%; margin-bottom: 3%">News.</h1>
<div class="row">
<div v-for="article in articles" class="col-md-3" style="margin-right: 5%; margin-bottom: 5%">
<b-card :title="article.title"
:img-src="article.urlToImage"
img-alt="Image"
img-top
tag="article"
class="mb-2 h-100">
<p class="card-text">
{{truncate(article.description)}}
</p>
<a :href="article.url" target="_blank" class="card-link">Read Full Story</a>
</b-card>
</div>
</div>
</div>
</div>
</template>
<script>
import axios from 'axios'
import Truncate from 'truncate'
export default {
data: function() {
return {
articles: {}
}
},
methods: {
loadArticles: function() {
var url = 'https://newsapi.org/v2/top-headlines?country=gb&apiKey='+'YOUR_KEY_HERE' // add your own API key
axios.get(url).then(response => {
this.articles = response.data.articles
})
},
truncate: function(description) {
return Truncate(description, 100)
}
},
mounted() {
this.loadArticles()
}
}
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment