In this tutorial, I will show you how to build a Vue.js 3 CRUD example to consume REST APIs, display and modify data using Axios and Vue Router.
More Practice:
–
–
Fullstack:
–
–
–
–
–
–
–
Typescript version at:
Vuetify version:
Serverless with Firebase:
–
–
Contents []
Overview of Vue.js 3 CRUD example
We will build a Vue.js front-end Tutorial Application in that:
Each Tutorial has id, title, description, published status.
We can create, retrieve, update, delete Tutorials.
There is a Search bar for finding Tutorials by title.
Here are screenshots of our Vue.js 3 CRUD Application.
– Create a Tutorial:
– Retrieve all Tutorials:
– Click on Edit button to update a Tutorial:
On this Page, you can:
change status to Published using Publish button
delete the Tutorial using Delete button
update the Tutorial details with Update button
– Search Tutorials by title:
The introduction above is for Vue 3 Client with assumption that we have a Server exporting REST APIs:
Methods
Urls
Actions
POST
/api/tutorials
create new Tutorial
GET
/api/tutorials
retrieve all Tutorials
GET
/api/tutorials/:id
retrieve a Tutorial by :id
PUT
/api/tutorials/:id
update a Tutorial by :id
DELETE
/api/tutorials/:id
delete a Tutorial by :id
DELETE
/api/tutorials
delete all Tutorials
GET
/api/tutorials?title=[keyword]
find all Tutorials which title contains keyword
All of them can work well with this Vue App.
Vue.js 3 Component Diagram with Vue Router & Axios
– The App component is a container with router-view. It has navbar that links to routes paths.
– TutorialsList component gets and displays Tutorials.
– Tutorial component has form for editing Tutorial’s details based on :id.
– AddTutorial component has form for submission new Tutorial.
– These Components call TutorialDataService methods which use axios to make HTTP requests and receive responses.
Technology
vue 3
vue-router 4
axios 0.21.1
bootstrap 4
Project Structure
Let me explain it briefly.
– package.json contains 4 main modules: vue, vue-router, axios, bootstrap.
– There are 3 components: TutorialsList, Tutorial, AddTutorial.
– router.js defines routes for each component.
– http-common.js initializes axios with HTTP base Url and headers.
– TutorialDataService has methods for sending HTTP requests to the Apis.
– vue.config.js configures port for this Vue Client.
Setup Vue 3 Project
Open cmd at the folder you want to save Project folder, run command:
vue create vue-3-crud
You will see some options, choose Default ([Vue 3] babel, eslint).
After the process is done. We create new folders and files like the following tree:
public
src
components
AddTutorial.vue
Tutorial.vue
TutorialsList.vue
services
TutorialDataService.js
App.vue
main.js
package.json
Add Bootstrap to Vue 3 CRUD example
Run command: npm install bootstrap jquery popper.js.
Open src/main.js and import Bootstrap as following-
import { createApp } from 'vue'
import App from './App.vue'
import 'bootstrap'
import 'bootstrap/dist/css/bootstrap.min.css'
...
Add Vue Router to Vue 3 CRUD example
– Run the command: npm install vue-router@4.
– In src folder, create router.js and define Router as following code:
Because most of HTTP Server use CORS configuration that accepts resource sharing restricted to some sites or ports, so we also need to configure port for our App.
In project root folder, create vue.config.js file with following content:
module.exports = {
devServer: {
port: 8081
}
}
We’ve set our app running at port 8081.
Run Vue.js 3 CRUD example
You can run our App with command: npm run serve.
If the process is successful, open Browser with Url: http://localhost:8081/ and check it.
Conclusion
Today we’ve built a Vue.js 3 CRUD example successfully with Axios and Vue Router. Now we can consume REST APIs, display and modify data in a clean way. I hope you apply it in your project at ease.
Happy learning, see you again!
Further Reading
Fullstack CRUD App:
Source Code
vue-3-crud-example-axios-create-tutorial
vue-3-crud-example-axios-retrieve-tutorial
vue-3-crud-example-axios-retrieve-one-tutorial
vue-3-crud-example-axios-update-tutorial
vue-3-crud-example-axios-search-tutorial
You can find step by step to build a Server like this in one of these posts:
–
–
–
–
–
–
–
–
–
–
–
–
–
–
vue-3-crud-example-axios-router-app-diagram
vue-3-crud-example-axios-project-structure
For more details about ways to use Axios, please visit:
You can add Pagination to this Component, just follow instruction in the post:
This Vue Client will work well with following back-end Rest APIs:
–
–
–
–
–
–
–
–
–
–
–
–
–
–