Hello everyone! Hope you are all set for the new year. While you are waiting for the Y2020 to end with bated breath, here’s a post to kick start your Vue 3 journey. We will create a Reddit reader using Vue3 and Vite!
This post is more useful for someone with basic knowledge of Vue and Vue 2.
Get Started: Installation
Create a Vue 3 project with Vite..
1
npm create @vitejs/app
Select the vue template on prompt to create a new folder for your project. Install dependencies.
1
2
cd reddit-reader-vite-vue-app
npm i
Run the project -
1
npm run dev
The project starts almost instantaneously.. sweet. Do two more things -
Open the project folder in VSCode
Open http://localhost:3000/ in your browser
Edit the file index.html and include reference to a light-weight CSS framework. I use chota css.
Create a blank file at src/assets/css/styles.css to create custom styles (we will include this file in main.js). I will not explicitly outline any custom styles here, but you should find them here on the Github repo.
Yay.. you have this beautiful app.
You are all set! Let us start coding in the app.
Setup Vue Router
We will have a bunch of links (well, two to be exact) to navigate within the app. Vue Router makes that routing real easy. Let’s set it up.
First, install Vue router compatible with Vue3.
1
npm i vue-router@4
Next, create the router file src/router/index.js -
We will use the same view Posts for both top and controversial posts. The view will decide which data to fetch based on the prop called filter, which gets passed by the router.
Change src/main.js to use the router file and include the custom style file we had created earlier -
<template><footerclass="text-grey bg-light"><divclass="container is-left">i am groot</div></footer></template>
Change Post View
Let’s go back to our posts view and do just one thing - outline a list of top and controversial posts. No pagination, showing the page details - nothing. We just take the user to Reddit when a link is clicked. We will use the all new Vue composition API and features.
Let’s start by extending the template created earlier to receive props from router, call Reddit API, and display the results.
<scriptsetup>constmounted=onMounted(async()=>{console.log("Fetching them posts..",props.filter);posts.value=awaitfetchPosts();console.log("posts.value: ",posts.value);});</script>