Check out a free preview of the full Building Applications with Vue & Nuxt course

The "Adding Composition API to Nuxt" Lesson is part of the full, Building Applications with Vue & Nuxt course featured in this preview video. Here's what you'd learn in this lesson:

Sarah demonstrates how the Composition API can be added to a Nuxt application, and explains the defineComponent, ref, and watchEffect modules.


Transcript from the "Adding Composition API to Nuxt" Lesson

>> What we're building now with Vue2, and keep in mind that this course is made at a particular point in time when U3 is just coming out and Nuxt is just supporting it. So, these things might move around in the future and in fact, I think in the future, you won't have to do any of this stuff.

But if you are working with it right now, here's what we would do to make this setup work in Nuxt. So I'm gonna go over to my terminal and I'm going to close this out. I'm gonna close this out and I'm gonna go up one directory, and I'm gonna say go into the compapi-start and we're gonna say yarn dev up, yarn.

And, we have our localhost here that's building. So what we have is this Count that we can increase and we have our Homepage. Let's look at what we have inside that homepage. So some major differences that we have coming in. We have to bring in TypeScript in order to use it with Nuxt currently, so I have a tsconfig.json so that I can work with TypeScript within Nuxt.

The next thing that we need to make sure of is that we have a plugin, where we bring in the composition-api. Again, this is not something that you probably have to do in the far future, but for now, we're going to create a plugin that imports Vue from vue, imports VueCompositionApi.

And it's so standard that we might need to do this that is actually my default V plugin for my VS code snippets, if you write V plugin, that's what it's gonna assume you want right now. We also have to install it. So we have to install @vue/composition-api. So we we're installing @vue/composition-api.

And then we're bringing it in through this plugin, which we will then register in nuxt.config. So if we go into nuxt.config, remember before we installed that plugin by saying plugins/tilda, plugins/composition-api, we're gonna do the same here. We also want to bring in the build module of @nuxt/typescript-build, so that we can make sure to work with Nuxt in TypeScript.

Now if we go into our page, this count is just the same, like as I mentioned before, in templates it's very similar. So this surface API for directives, and the templates have not changed in composition-api. Here what I wanna do is I wanna actually defineComponent. I need to say script lang equals ts.

I just need to say import defineComponent, ref and watchEffect. So anything that I'm using, I'm gonna bring in and I'm gonna say export default defineComponent. Here I will pass in setup, and there I will say like const count = ref is 0. You can also use reactive as we used before.

If I wanted to addToCount I say count.value, if I was using reactive it would be state dot whatever the thing is that you decided, so state.count. We also have a thing called watchEffect which is similar to watch. So, in this, we have access to any watched properties. The only difference between watchEffects and watchers are not the only difference, few differences between watchEffects and watchers from the options API is, you'll note that I don't have to declare what thing I'm watching.

I'm actually just watching everything, so I can access those values willy nilly. That's not a technical term, but I can access whatever I want, that's either setup in refs, or reactive, or props, or what have you. And I don't have to say like in watch for the options API, I would have had to say count.

Here I'm not saying that I'm saying console.log(count.value). And remember, we've got to return. So we've got to return the count. And we've got to return any function we want executed. We don't have return our watcher because our watcher is part of the reactivity system. So if I look here in Vue, so we've got our console.log, this was the other change between watch and watchEffect.

WatchEffect runs immediately, whereas watchers, once you change it, it's invoked. For watchEffect, you can see that zero happening right away. That zero is happening right away because, as soon as it's instantiated, that watchEffect is gonna run and anything inside of it is gonna run as well. So now, from here I can Increase the Count.

And you can see that property change.

Learn Straight from the Experts Who Shape the Modern Web

  • In-depth Courses
  • Industry Leading Experts
  • Learning Paths
  • Live Interactive Workshops
Get Unlimited Access Now