Image for post
Image for post

Vue.js 2 & Vue-Resource- Real-World Vue Application With External API Access

This post has been published first on

Vue.js 2 is great for building powerful single-page web applications. In the following we’re going to build a real-world Vue.js 2 application which makes use of an external REST API from By using this free API we’re able to retrieve latest news articles from popular news sources and blogs. To access this external API we’re going to use the vue-resource packages which provides a service for making web requests and handle responses.

What We’re Going To Build

In the following screenshot you can see what the application we’re going to build looks like:

Image for post
Image for post

The user is able to select a news source from a drop-down list. Having selected a news source the news article list at the bottom is automatically updated with the latest news articles from this source. Clicking on the headlines takes the user to the original website of the article.

Project Setup

The easiest way to initiate a new Vue.js 2 project is by using Vue CLI. On the command line just execute the follwing command:

If you haven’t installed Vue CLI before on your system you can do so by using the following command first:

This makes sure that Vue CLI is installed globally in your system and that the command is available.

Having initialized the application you need to change into the new project directory vuenews and execute the following command to download and install all standard project dependencies:

After the installation has been completed successfully you can start the development web server via:

Include Bootstrap

In index.html include Bootstrap (and jQuery) from CDN:

We’ll use the Bootstrap CSS classes for styling the user interface of our application.

Adding vue-resource To The Project

The vue-resource plugin for Vue.js provides services for making web requests and handle responses using a XMLHttpRequest or JSONP.

For our sample application we’d like to retrieve data from which is a simple and easy-to-use API that returns JSON metadata for the headlines currently published on a range of news sources and blogs. By using vue-resource we’re able to use this API within our Vue.js 2 application.

First add vue-resource to the project by using the following command:

Next, open file main.js and insert the following import statement:

In addition add the following line of code right under the import statements:

Now we're ready to use vue-resource within our components.

Implementing App Component

Let’s start with the implementation in component App. Open file App.vue and adapt the source code as you can see in the following:

As we would like to split up our application in two components (SourceSelection and Newslist) we first need to add import statements for both components in the script section. For both components new files will be added in the src/components later on.

We also need to add SourceSelection and Newslist to the object which is assigned to the components property of the component’s configuration object.

Next, we can make use of both components within the template code:

By including and within the template of App component we make sure that the HTML output which is generated by both componnets is inserted into the main application template and included in the browser output.

For the element we're attaching the event handler method sourceChanged to the event type sourceChanged. This is a custom event of component SourceSelection and will be emitted each time the user selects a new news source. The event handler method is added to the methods object and makes sure that the source property of App component is updated with the new source value from the user selection.

Later on, when implementing the Newslist component we'll define an input property named source. By using this property we're able to pass in the current news source which should be used to retrieve and display a list of news headlines in component Newslist.

To make sure that this input property is always up-to-date we're binding it to the value of the source property of App component by using the v-bind directive.

Implementing SourceSelection Component

Next, let’s implement the SourceSelection component in the new file src/components/SourceSelection.vue. The complete source code is shown in the following listing:

The template implementation consists of a select from control which gives the user the possibility to choose from a list of news sources. The options which are presented in the list are taken from the sources array. By using the v-for directive in the element we're able to iterate over the elements of this array and generate an element for each entry.

Furthermore we're sonnecting the change event of the select element to the sourceChanged event handler by using the v-on directive in the following form: .

The sourceChanged method is added to the object which is assigned to the methods property of the component's configuration object. A for loop is used to iterate over the sources array and compare the source id with the id of the source the user has selected. If a match is found the property of the component is set to this source element. Furthermore the custom component event sourceChange is emitted and the selected source id is passed in as an event parameter. Emitting this custom element each time the user selection of a news source changes is the prerequisite to be able to subscribe to this to this event from outside of the component like we did in App.vue:

Because we’re using the sources array to get a list of all available news sources we need to make sure that this array is filled when to component is created. The right place to do that is the lifecycle hook created. We we need to add created as a property to the component’s configuration object and then assign a function which contains code to initialize the array.

We’re using the $http object from the vue-resource package to send a GET request to the Newsapi service which returns a list of news sources ( To send a GET reques to this service endpoint the get method of the $http object is used. This method returns a promise so that we can add the then method to attach a function which is executed when the promise is resolved (the response from the GET request is available). The list of sources which is returned from the web service is available in, so that we can these values to this.source.

Implementing Newslist Component

Next, let’s continue with the implementation of Newslist component. Newslist component is included in the template of component App to generate a list of news headline from the news source selected:

For the implementation of Newslist we create a new file Newslist.vue in src/components/ and insert the following code:

In order to be able to use the source attribute in element we need to define a corresponding component property by using the props property:

With that inbound property available, we now have access to the current news source. We are able to use that piece of information to send another GET request to the Newslist API in oder to retrieve latest news items. To make sure that this request is always executed when the source value changes we establish a watcher:

This watcher function is executed each time the value of source changes and is calling the method updateSource. The implementation of updateSource is added to the object which is assigned to methods and contains the code which is needed to retrieve the latest news from the service and assign the results to the articles array. In this case the service endpoint is We also need to add two URL parameters to this service endpoint. The news source we would like to use must be added with URL paramater source. Another parameter which needs to be added is apiKey. The value which we need to assign to this parameter is the API key which can be retrieved from the Newsapi website ( after registration.

Again, the call of the get method returns a promise, so that we can use the then method to attach a function which is called each when the promise is resolved. In that case we’re assigning the result of the service request (available in to the array articles.

With that prerequisite we’re able to implement the component’s template and use the articles array within the template to generate the HTML output to display a list of news articles to the user.

Video Tutorial

This video tutorial contains the steps described in the text above:

Also check out the great online course: Vue.js 2 — The Complete Guide

This post has been published first on Blog

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store