Vue 3 with Typescript – 4 to 5

download demo

Types and Props

inside components folder, create JobsList.vue

type “ts” + click on bottom option

There should be boilerplate code. Let’s add template and style.

Now we declare and import in file App.vue.
then put it inside of components object to use it.

get rid of our placeholder template code and replace it with our component. The jobs data that we have in setup should now be passed inside the props as well:

Implementing JobList

We make an object for props property. Then declare jobs property. This property is the options of how jobs is declared. We say it is required, and of type Array.

In our template, we put the necessary html and a ul. For the li, we cycle through jobs using v-for. It needs a unique key prop. We have it in jobs data array as id.

JobsList.vue

If you were to run the site, you’ll see the list of titles.

Next we declare props for the component. If you were to use say Array for prop jobs, it doesn’t know what properties are available for each job because in props jobs declaration, we only declared it
as Array.

JobsList.vue

Hence, no code suggestions when you use it in template.

The solution is that we need to type our props better.

Using Vetur for template interpolation

JobsList.vue

Now, you’ll be able to see different options when you try to access properties of job object.
In settings, make sure Vetur > Experimental: Template Interpolation Service is checked

JobsList.vue

Functions

ref – https://www.youtube.com/watch?v=usSBsgWNUZk

We want jobs to be reordered by salary, title..etc.

add event handler to each of them.

create file types/OrderTerm.ts

first create this function

App.vue

First look at order reference. We pass it into JobList so that when a button is pressed, an event happens, and manipulates the order ref.
The change in order ref can be evaluated in JobList.

Since it has been set up in App.vue, we must implement this in JobList.vue

We declare order prop.

components/JobList.vue

in the template:

App.vue