DEV Community

loading...
Cover image for HackerNews Reader with Vue 3, Vite 2, VueX 4 & Tailwind — Part 1

HackerNews Reader with Vue 3, Vite 2, VueX 4 & Tailwind — Part 1

infantiablue profile image Truong Phan ・3 min read

VueJS is raising as one of the most popular front end framework, compared with React (supported by Facebook) and Angular (from Google). Recently, it has been updated to version 3 with many new exciting features. In this post, we will explore the combination with VueX (state management) to handle 3rd party API. To make it simple for the learning purpose, our goal is just to receive the top articles from Hacker News and load it from the client side.

You can try the online demo here https://vhnews.netlify.app/

First of all we use Vite to scaffold the project. You may wonder why I don’t use the official Vue CLI tool. The reason is Vite is really fast, and in this case I just want to make a quick demonstration. Vue CLI, in other hand, is built on top of the powerful and popular Webpack , will bring you an amazing plugin ecosystem (and it’s compatible with Vue 2). So, now we use yarn (you can use npm instead, just a personal favor, although I prefer the speed of yarn) to create our new web app (requires Node.js version >=12.0.0.)

yarn create @vitejs/app
Enter fullscreen mode Exit fullscreen mode

After enter the command, you will be prompted to choose some selections. Then we cd to your working directory and run following commands to install some tools: VueX (version 4.x), eslint as well as its plugin for Vue and axios.

yarn
yarn add axios vuex@next --save
yarn add -D eslint eslint-plugin-vue
yarn eslint --init
yarn dev
Enter fullscreen mode Exit fullscreen mode

Now, you can open the browser and go to the address http://localhost:3000 to see if the dev server is running.

The dev server is running at localhost:3000

For the interface, I gonna use Tailwind, and “Vue 3 and Vite don’t support PostCSS 8 yet so you need to install the Tailwind CSS v2.0 PostCSS 7”.

yarn add -D tailwindcss@npm:@tailwindcss/postcss7-compat @tailwindcss/postcss7-compat postcss@^7 autoprefixer@^9
Enter fullscreen mode Exit fullscreen mode

Next, to generate the tailwind.config.js and postcss.config.js files, run:

npx tailwindcss init -p
Enter fullscreen mode Exit fullscreen mode

From the official guide: “In your tailwind.config.js file, configure the purge option with the paths to all of your pages and components so Tailwind can tree-shake unused styles in production builds.”

module.exports = {
    purge: ['./index.html', './src/**/*.{vue,js,ts,jsx,tsx}'],
    darkMode: false, // or 'media' or 'class'
    theme: {
      extend: {},
    },
    variants: {
      extend: {},
    },
    plugins: [],
}
Enter fullscreen mode Exit fullscreen mode

Then create a new file main.css in src/assets/css:

/* ./src/assets/css/main.css */

/*! @import */
@tailwind base;
@tailwind components;
@tailwind utilities;
Enter fullscreen mode Exit fullscreen mode

Then, we need to fetch the data from HackerNews to VueX store first. In the snippet below, I also set up the axios instance, so that we can re-use it later. The API from HackerNews to get top stories only return the IDs, so that we need to fetch each individual item after receiving the arrays.

Next, we create a new component at components/Stories.vue as below:

Then add VueX to the main.js

import { createApp } from "vue";
import App from "./App.vue";
import store from "./store";
import "./assets/css/main.css";
const app = createApp(App);
app.use(store);
app.mount("#app");
Enter fullscreen mode Exit fullscreen mode

Finally, we edit App.vue

Open the http://localhost:3000 and voilà.

![Top stories from Hacker News(https://dev-to-uploads.s3.amazonaws.com/i/hvhu4g5gnpfii0os2xwk.png)

Hmm, I forgot the time, we need to make it more readable, instead of a string of numbers. I gonna use the timeago.jspackage to manipulate.

yarn add timeago.js
Enter fullscreen mode Exit fullscreen mode

Then, we add a new method in components/Stories.vue:

methods: {
  parseTime(t) {
    return timeago.format(t * 1000);
  }
},
Enter fullscreen mode Exit fullscreen mode

and implement it in template section:

<div class="text-sm text-gray-500">{{ parseTime(item.time) }}</div>
Enter fullscreen mode Exit fullscreen mode

Reload the page to check the result

The time is readable

The final source code is on Github repo.
In the next article, we will implement advanced features of Vue components to render them dynamically. I would appreciate to receive any feedback from you guys

Resources:
Vite.JS
Vuex@Next
Official Hacker News API
Tailwind CSS

Discussion (2)

pic
Editor guide
Collapse
zynth17 profile image
Christopher Reeve

is there any way to use jsx with this?

Collapse
infantiablue profile image
Truong Phan Author

Yes, I think it's possible since Vue 3 has supported jsx pretty well