Quickstarters
How to Build a Vue Frontend and Connect it to a Backend?
33 min
in this tutorial, you’ll build a to do list application using vue and connect it to a managed backend service this guide is perfect for you if you want to master essential crud (create, read, update, delete) operations and build an intuitive user interface by the end, your app will fully interact with a backend powered by back4app developing a full stack application can involve complex backend setups, database management, and user authentication to let you focus on crafting an outstanding vue frontend, we’ll use back4app to manage the backend effortlessly back4app supplies a ready to use database, authentication, cloud code for custom server logic, and sdks to integrate with your app this allows you to deploy scalable applications without the hassle of server maintenance key takeaways by following this tutorial, you will set up a modern vue project using industry standard tools seamlessly integrate a backend service to handle your application’s data implement essential crud operations for a dynamic to do list deploy your fully functional application via containerized workflows on back4app prerequisites before you begin, make sure you have node js and npm installed on your machine verify installations with node v and npm v basic vue knowledge , including components, reactive data, and handling events familiarity with vue 3’s composition api is beneficial a back4app account to manage your backend services register at back4app https //www back4app com/ if you haven’t already with these in place, you’re ready to set up your project project setup start by preparing your local development environment and initializing your vue project with vite for a fast and modern build experience verify you have node js (lts version) installed if not, download it from nodejs org https //nodejs org/ create your vue project by running npm create vite\@latest todo app 3\ change to your project directory cd todo app 4\ install dependencies npm install 5\ launch the development server to ensure everything works npm run dev open the provided url in your browser with your vue frontend ready, the next step is to set up your backend on back4app creating the todo backend back4app offers a fully managed backend service powered by parse , complete with a nosql database, user authentication, cloud code, and automatic api generation follow these steps to create a backend log in to your back4app dashboard https //www back4app com/ and click "create a new app " name your app (for example, todoapp ) and select nodejs/parse as the backend type go to "database" > "browser" , click "create a class" , and select "custom" name the class task and set the permissions to allow public read and write (you can refine these later) in the task class, add the following fields title (string) – the task title description (string) – details about the task completed (boolean) – status of task completion duedate (date) – when the task is due click "save" to create the schema integrating back4app with vue you’ll use the parse javascript sdk to communicate between your vue frontend and the back4app backend install the parse sdk npm install parse configure the sdk by editing src/main js import the sdk and initialize it with your application id and javascript key from the back4app dashboard (found under app settings > security & keys ) import { createapp } from 'vue'; import app from ' /app vue'; import parse from 'parse/dist/parse min js'; const parse app id = 'your application id'; const parse js key = 'your javascript key'; const parse server url = 'https //parseapi back4app com/'; parse initialize(parse app id, parse js key); parse serverurl = parse server url; createapp(app) mount('#app'); with your backend connected, you can now build the to do list interface and integrate crud operations developing the frontend your vue application will consist of components to add, display, update, and remove tasks you will leverage vue’s reactivity to manage the state and ensure smooth updates structuring your components create a components folder inside src to house your vue components mkdir src/components touch src/components/taskform vue src/components/tasklist vue src/components/taskitem vue taskform vue this component handles adding new tasks it uses a controlled form to capture user input \<template> \<form @submit prevent="handlesubmit"> \<input type="text" placeholder="task title" v model="title" required /> \<input type="text" placeholder="description" v model="description" required /> \<button type="submit">add task\</button> \</form> \</template> \<script> import parse from 'parse/dist/parse min js'; export default { name 'taskform', props \['fetchtasks'], data() { return { title '', description '' }; }, methods { async handlesubmit() { try { const task = parse object extend('task'); const task = new task(); task set('title', this title); task set('description', this description); task set('completed', false); await task save(); this fetchtasks(); this title = ''; this description = ''; } catch (error) { console error('error adding task ', error); } } } }; \</script> tasklist vue this component displays the list of tasks and integrates task actions \<template> \<div> \<p v if="!tasks length">no tasks available\</p> \<taskitem v for="task in tasks" \ key="task id" \ task="task" \ fetchtasks="fetchtasks" /> \</div> \</template> \<script> import taskitem from ' /taskitem vue'; export default { name 'tasklist', components { taskitem }, props \['tasks', 'fetchtasks'] }; \</script> taskitem vue this component represents an individual task and includes actions to mark as complete or delete \<template> \<div \ class="\['task item', { completed task completed }]"> \<div> \<h3>{{ task title }}\</h3> \<p>{{ task description }}\</p> \</div> \<div> \<button @click="handlecomplete"> {{ task completed ? 'undo' 'complete' }} \</button> \<button @click="handledelete">delete\</button> \</div> \</div> \</template> \<script> import parse from 'parse/dist/parse min js'; export default { name 'taskitem', props \['task', 'fetchtasks'], methods { async handlecomplete() { try { const query = new parse query('task'); const tasktoupdate = await query get(this task id); tasktoupdate set('completed', !this task completed); await tasktoupdate save(); this fetchtasks(); } catch (error) { console error('error updating task ', error); } }, async handledelete() { try { const query = new parse query('task'); const tasktodelete = await query get(this task id); await tasktodelete destroy(); this fetchtasks(); } catch (error) { console error('error deleting task ', error); } } } }; \</script> managing state in app vue in your main component, you’ll manage the list of tasks using vue’s reactive data and lifecycle hooks \<template> \<div class="container"> \<h1>to do list\</h1> \<taskform \ fetchtasks="fetchtasks" /> \<tasklist \ tasks="tasks" \ fetchtasks="fetchtasks" /> \</div> \</template> \<script> import { ref, onmounted } from 'vue'; import parse from 'parse/dist/parse min js'; import taskform from ' /components/taskform vue'; import tasklist from ' /components/tasklist vue'; export default { name 'app', components { taskform, tasklist }, setup() { const tasks = ref(\[]); const fetchtasks = async () => { try { const task = parse object extend('task'); const query = new parse query(task); const results = await query find(); tasks value = results map(task => ({ id task id, task tojson() })); } catch (error) { console error('error fetching tasks ', error); } }; onmounted(fetchtasks); return { tasks, fetchtasks }; } }; \</script> styling the application create a styles css file in src to add basic styling container { max width 600px; margin 40px auto; padding 0 20px; text align center; font family sans serif; } container h1 { margin bottom 20px; } form { display flex; flex direction column; align items center; gap 10px; margin bottom 20px; } form input\[type="text"] { width 80%; max width 400px; padding 8px; box sizing border box; font size 1rem; } form button { padding 8px 16px; cursor pointer; font size 1rem; border none; background color #eaeaea; transition background color 0 2s ease; } form button\ hover { background color #ccc; } task item { display flex; flex direction column; align items center; justify content center; gap 12px; border 1px solid #ccc; border radius 6px; padding 15px; margin 10px 0; background color #fafafa; text align center; transition background color 0 2s ease; } task item completed h3, task item completed p { text decoration line through; color #888; } task item h3 { margin 0; font size 1 1rem; } task item p { margin 0; font size 1rem; } task item button { padding 6px 12px; border none; background color #eaeaea; cursor pointer; font size 0 9rem; } task item button\ hover { background color #ccc; } @media (min width 600px) { task item { flex direction row; } } import the stylesheet in main js import ' /styles css'; deploying the frontend on back4app web deployment back4app web deployment lets you containerize your vue application for production with docker configuring vite for production adjust vite config js to set the base path correctly for a containerized environment import { defineconfig } from 'vite'; import vue from '@vitejs/plugin vue'; export default defineconfig({ plugins \[vue()], base ' /', }); generate the production build npm run build creating a dockerfile create a dockerfile in the root directory \# stage 1 build the vue app from node 18 alpine as build workdir /app copy package json package lock json / run npm install copy run npm run build \# stage 2 serve the app using nginx from nginx\ alpine copy from=build /app/dist /usr/share/nginx/html expose 80 cmd \["nginx", " g", "daemon off;"] testing the docker container locally build your docker image docker build t todo frontend run the container docker run p 8080 80 todo frontend visit http //localhost 8080 to verify that your vue app is served correctly deploying to back4app initialize a git repository, add your files, and commit git init git add git commit m "initial commit for back4app deployment" git branch m main git remote add origin \<your github repository url> git push u origin main log in to back4app web deployment https //www back4app com/containers click "create new app" , provide a project name, and select github repository authorize back4app to access your repository and choose dockerfile deployment confirm build settings and click "deploy" to trigger the first build monitoring your deployment after deployment, use the back4app dashboard to view logs for debugging monitor resource usage and container restarts redeploy on new commits using the "trigger build" option configure custom domains if needed access the live application at https //todoapp ku5ofg8s b4a run/ https //todoapp ku5ofg8s b4a run/ https //todoapp ku5ofg8s b4a run/ testing and debugging after deploying, ensure your vue frontend communicates correctly with the back4app backend verify api calls open your browser’s developer tools (f12 → network) to inspect api requests add and retrieve tasks use the app interface to add tasks and check the back4app database browser crud operations test completing and deleting tasks, then confirm the changes in the backend edge cases validate form inputs and simulate slow network conditions (using chrome devtools) to assess performance troubleshooting common issues cors errors update allowed headers and domains in back4app (dashboard > app settings > security & keys) to include your frontend url authentication errors (401) confirm your application id and javascript key are correct backend deployment issues check container logs in the back4app dashboard and restart the container if necessary best practices for using back4app with vue to optimize your application efficient data requests use batch requests for multiple operations const tasks = \[task1, task2, task3]; parse object saveall(tasks); optimize queries request only necessary fields query select('title', 'completed'); environment variables store sensitive keys in a env file vite parse app id=your app id vite parse js key=your js key security use acls to restrict access task setacl(new parse acl(parse user current())); cloud code offload complex logic to backend functions to reduce api exposure conclusion you have successfully built a full stack to do list application by setting up a vue frontend, integrating it with back4app’s backend, and deploying it via containerized workflows this tutorial demonstrated a streamlined path from local development to cloud deployment, ensuring smooth interaction between your vue ui and backend services looking ahead, consider enhancements like advanced user management, real time updates, and integration of third party services for more information, visit the back4app documentation https //www back4app com/docs and explore community resources