Your resource for web content, online publishing
and the distribution of digital products.
S M T W T F S
 
 
 
 
 
1
 
2
 
3
 
4
 
5
 
6
 
7
 
8
 
9
 
 
 
 
 
 
 
 
 
18
 
19
 
20
 
21
 
22
 
23
 
24
 
25
 
26
 
27
 
28
 
29
 
30
 

Augmenting the Client With Vue.js

DATE POSTED:September 26, 2024

\ In my previous post, I laid the ground to build upon; now is the time to start "for real".

\ I heard a lot of Vue.js. Additionally, a friend who transitioned from developer to manager told me good things about Vue, which further piqued my interest. I decided to have a look at it: it will be the first "lightweight" JavaScript framework I'll study - from the point of view of a newbie, which I am.

Laying out the work

I explained WebJars and Thymeleaf in the last post. Here's the setup, server- and client-side.

Server-side

Here is how I integrate both in the POM:

\

org.springframework.boot spring-boot-starter-web org.springframework.boot spring-boot-starter-thymeleaf org.webjars webjars-locator 0.52 org.webjars.npm vue 3.4.34
  1. Spring Boot itself; I decided on the regular, non-reactive approach
  2. Spring Boot Thymeleaf integration
  3. WebJars locator, to avoid specifying the Vue version on the client-side
  4. Vue, finally!

\ I'm using the Kotlin Router and Bean DSLs on the Spring Boot side:

\

fun vue(todos: List) = router { //1 GET("/vue") { ok().render("vue", mapOf("title" to "Vue.js", "todos" to todos)) //2-3 } }
  1. Pass a static list of Todo objects
  2. See below
  3. Pass the model to Thymeleaf

\ If you're used to developing APIs, you're familiar with the body() function; it returns the payload directly, probably in JSON format. The render() passes the flow to the view technology, in this case, Thymeleaf. It accepts two parameters:

\

  1. The view's name. By default, the path is /templates and the prefix is .html; in this case, Thymeleaf expects a view at /templates/vue.html
  2. A model map of key-value pairs
Client-side

Here's the code on the HTML side:

\

  1. Axios helps making HTTP requests
  2. Vue itself
  3. Our client-side code
  4. Set the data

\ As explained in last week's article, one of Thymeleaf's benefits is that it allows both static file rendering and server-side rendering. To make the magic work, I specify a client-side path, i.e., src, and a server-side path, i.e., th:src.

\

The Vue code

Now, let's dive into the Vue code.

We want to implement several features:

\

  1. After the page load, the page should display all Todo items
  2. When clicking on a Todo completed checkbox, it should set/unset the completed attribute
  3. When clicking on the Cleanup button, it deletes all completed Todo
  4. When clicking on the Add button, it should add a Todo to the list of Todo with the following values:
  • id: Server-side computed ID as the max of all other IDs plus 1
  • label: value of the Label field for label
  • completed: set to false
Our first steps into Vue

The first step is to bootstrap the framework. We have already set up the reference for our custom vue.js file above.

\

document.addEventListener('DOMContentLoaded', () => { //1 // The next JavaScript code snippets will be inside the block }
  1. Run the block when the DOM has finished loading

\ The next step is to let Vue manage part of the page. On the HTML side, we must decide which top-level part Vue manages. We can choose an arbitrary

and change it later if need be.

\

\ On the JavaScript side, we create an app, passing the CSS selector of the previous HTML

.

\

Vue.createApp({}).mount('#app');

\ At this point, we launch Vue when the page loads, but nothing visible happens.

\ The next step is to create a Vue template. A Vue template is a regular HTML