In this article, we will learn about basic concepts of Vue.js and how to install Vue.js and create a new project. We will also learn how to perform HTTP Get operation to fetch data in a Vue.js application.
What is Vue.js
Vue.js is an open-source front-end JavaScript framework for building user interfaces and single-page applications (SPAs). Vue.js is commonly referred to as Vue. and pronounced as View.
As per official Vue.js documentation,
Vue is a progressive framework for building user interfaces. Unlike other monolithic frameworks, Vue is designed from the ground up to be incrementally adoptable. The core library is focused on the view layer only, and is easy to pick up and integrate with other libraries or existing projects. On the other hand, Vue is also perfectly capable of powering sophisticated Single-Page Applications when used in combination with modern tooling and supporting libraries.
Vue was created by Evan You, who was then working for Google using AngularJs in their projects
Prerequisites
- Basic knowledge of HTML and JavaScript.
- Text editor (We use Visual studio code)
Vue.js Installation
In this article, we will use Vue CLI to install and create vue project.
We use the following command to install Vue.js using CLI.
Create Vue.js Project
Create a Vue.js project by using the following command.
Now Install the Axios library using the following command. Learn more about
Axios.
Install bootstrap using the following command.
- npm install bootstrap-vue bootstrap --save
Open main.js file and import bootstrap reference.
- import 'bootstrap/dist/css/bootstrap.css'
- import 'bootstrap-vue/dist/bootstrap-vue.css'
Now right click on the Src folder and add a new file named 'Http-comman.js'.and add following code in this file.
- import axios from "axios";
-
- export default axios.create({
- baseURL: "https://localhost:44314/",
- headers: {
- "Content-type": "application/json"
- }
- });
Now right click on the Src folder and add a new file named 'Employeeservice.js'.and add following code in this file.
- import http from "./Http-comman";
- class EemployeeService {
-
- getAll() {
- return http.get("/api/EmployeeInfo/Getemployeeinfo");
- }
-
- }
- export default new EemployeeService();
Now right click on the Src folder > components folder and create a new componnet named 'Employeedata.vue'.and add following code in this file.
- <template>
- <div class="row" style="margin-bottom: 10px;">
- <div class="col-sm-12 btn btn-success">
- How to fetch data from WEB API in Vue.js Application
- </div>
- </div>
- <div>
- <table class="table">
- <thead>
- <th>Id</th>
- <th>Name</th>
- <th>City</th>
- <th>Age</th>
- <th>Technology</th>
- <th></th>
- </thead>
- <tbody>
- <tr v-for="item in employees" :key="item.Id">
- <td>{{item.Id}}</td>
- <td>{{item.Name }}</td>
- <td>{{item.City }}</td>
- <td>{{item.Age}}</td>
- <td>{{item.Tech}}</td>
- </tr>
- </tbody>
- </table>
- </div>
- </template>
-
- <script>
- import EemployeeService from "../Employeeservice";
- export default {
- name: 'Employeelist',
- data() {
- return {
- employees: [],
- };
- },
- created()
- {
- this.retrieveemployees();
- },
-
- methods: {
- retrieveemployees() {
- EemployeeService.getAll().then(response => {
- this.employees = response.data;
- console.log(response.data);
- })
- .catch(e => {
- console.log(e);
- });
- },
-
- }
- }
- </script>
-
- <style lang="scss" scoped>
-
- </style>
Now open App.vue file and add the following code.
- <template>
- <Employeelist/>
- </template>
- <script>
- import Employeelist from './components/Employeedata.vue';
- export default {
- name: 'App',
- components: {
- Employeelist
- }
- }
- </script>
-
- <style>
- #app {
- font-family: Avenir, Helvetica, Arial, sans-serif;
- -webkit-font-smoothing: antialiased;
- -moz-osx-font-smoothing: grayscale;
- text-align: center;
- color: #2c3e50;
- margin-top: 10px;
- }
- </style>
Create a Web API Project
Open Visual Studio and click on create a new project,
Now select the project and click on the Next button.
Now select the project name and project location and click on the Create button.
Choose the template as Web API.
Right-click the Models folder from Solution Explorer and go to Add >> New Item >> data
Click on the "ADO.NET Entity Data Model" option and click "Add".
Select EF Designer from the database and click the "Next" button.
Add the connection properties, select the database name on the next page, and click OK.
Check the "Table" checkbox. The internal options will be selected by default. Now, click the "Finish" button.
Right-click on the Controllers folder and add a new controller. Name it "Employee Info controller" and add the following namespace in the Employee Info controller.
- using EmployeeInfo.Models;
Now, add a method to fetch data from the database.
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Net;
- using System.Net.Http;
- using System.Web.Http;
- using EmployeeInfo.Models;
-
- namespace EmployeeInfo.Controllers
- {
- public class EmployeeInfoController : ApiController
- {
- EmployeeEntities DB = new EmployeeEntities();
- [HttpGet]
- public object Getemployeeinfo()
- {
- return DB.EmployeeInfoes.ToList();
- }
- }
- }
Now, let's enable CORS. Go to Tools, open NuGet Package Manager, search for CORS, and install the "Microsoft.Asp.Net.WebApi.Cors" package. Open Webapiconfig.cs and add the following lines:
- EnableCorsAttribute cors = new EnableCorsAttribute("*", "*", "*");
- config.EnableCors(cors);
Now, go to Visual Studio code and run the project by using the following command: 'npm run serve'
Summary
In this article, we learned how to fetch data using web API and Vue.js. You can download the code from
Github.