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

The "Composition API Solution" 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 walks through the solution to the Composition API exercise.


Transcript from the "Composition API Solution" Lesson

>> So we have the index.view that we went over before. As you might assume, we have a layout.view with an app menu. In our app menu, since we're not doing anything in the app, there's no kind of script stuff going on, it really lays out the same way that it would for a typical view app, an options API app, so nothing new there.

We're pointing to tacofriends. And we have tacofriends.view as our page. So we have this main, we have our tacofriends, and all of this is kind of options API, or not options API, but the directives that you're used to from before. So we've got at click, alertunit. We've got v-for friend in friends, and key is friend.

Now if we go into the TypeScript section, you can see we have that define component. I'm showing you with reactive but like I said, you could have used ref. Because I'm using reactive, I'm going to also use two refs so that I can pass that in and say, state, and not have to say state dot, state dot.

Here inside the state, for reactive, we have list of friends and it's the Sarah likes all tacos, Brian Holt, making fun of Brian Holt, And Shirley, because I love them. Then we have this function called alert unit. And something you might have gotten caught on. I'm not sure if you're familiar with TypeScript is, that this e does need to have a type of property.

So like, if you're familiar working with TypeScript, you will probably use the correct designation here and say, string. But I show with any here, because just in case you're not familiar with TypeScript and you don't want to dive into all of TypeScript, there is a really great TypeScript course on front in masters.

Then use any, for right now, and in the future, this will upgrade so that you don't have to pass a type or use TypeScript. So we're gonna alert This is something that hangs off of that target. There's a number of ways we could have done this, right?

We could have actually stored an empty string inside of reactive or refs, and then had that click events pass through, and hold that, and alerted that instead. So if that's what you did, that's totally correct as well. We did that in the game. We also did something like it in the app.

So I'm just showing you another way of working here, but both are valid. And here, of course, this like special composition API thing of needing to pass the reactive state. So we're passing in the state to ref state, and then we're also passing in alert unit. So any kind of functions that we declare or something like that will also be passed in the return.

And again, that return statement is very explicit. It tells any person who comes along later. That is exactly what we're returning here, and that is what you can compose with. So there we have it. We have our app with our, Sarah likes all tacos, and then in home, we have our watch effect.

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