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

The "Create a Dynamic Page" 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 dynamic pages are created in Nuxt. The dynamic page accesses the URL parameters through the $route object, and filters the data based on the unique ID passed in the URL.


Transcript from the "Create a Dynamic Page" Lesson

>> So let's go back to our checklist. All right, we filtered the grid with a reusable component and computed properties, now, we're gonna create a dynamic page. So okay, let's create a dynamic page. We go back over to VS Code. And what we're gonna do in order to create a dynamic page, this part is really cool.

You're gonna be into it. You create a new thing with an underscore. So I'm going to use whatever I think is unique. Remember how we're using keys and before, we wanna use whatever is unique. So whatever is unique about that data, typically it's going to be an ID, but I've seen people use like, post slugs, and all sorts of things.

So I'm gonna say id.vue, I go in the Pages Directory, and then if I want it to be in a subdirectory, I'll say Items. So I'm gonna create a subdirectory directory called items. I typically do that for dynamic pages, because I want it to be very clear that this is a subdirectory, dynamic pages are happening inside here.

And so, I say _id.vue, I save, I say, vbase. And right away what I have access to is, if I make data I can say id: this.$ And this will give me access to whichever unique piece of data I'm using at that moment. And sure enough, we're gonna do that pretag up here again.

And I'm going to do the same thing I did last time where I kind of put the data on the page so that I can see it right away. But I wanna have a way to get there too, because I didn't memorize all of those long IDs when I was working with all of this stuff, sorry about that.

So I'm gonna go over to the components, and I'm gonna go to our AppRestaurantInfo. And remember this Ghost button that we never linked up, we wanna be able to access the item from that view item. So I'm gonna say vnuxt l, that's gonna give me the next link.

I'm gonna take this out and wrap this button. And then I'm going to use a template literal. And remember I said Item, I think, items, and then I'm going to pass in, because that's what I used up there was the The last thing I wanna do is I wanna make sure that I bind this so that it's not just, cuz remember, otherwise it's just going to use a string.

That's all of this and that's going to be garbage, so we definitely want to change that. And for this, we also want to restart the server, so we're gonna go over here. Go restart the server, because it needs to generate all of those pages. So I need to restart the server so it can create all of these dynamic pages for me.

So if I go over to this refresh, then I should be able to go to the page and you'll see localhost items and the ID of the item. And you can see that ID outputted to the page. The last thing we're gonna do before I give you an exercise is, right now I have the ID and that is all I have.

I have the unique ID for the thing. But in that unique ID for the thing, I don't know, all of the other information about it right, I don't know the item title, I don't know the price, I don't know all of these other pieces. So what I'm gonna do is I'm gonna go back and I'm going to make sure that I'm going through all of our food data and I'm grabbing one of those pieces of the array from that ID.

So I'm gonna filter through that information. So I'm gonna go to the ID and I'm going to say, vmapstate, and I'm going to bring in, I always put computed after data. And I'm gonna say fooddata. And now, what I want to do here is I wanna actually make a new thing in computed properties that I'm gonna call currentItem, not cuttent item, currentItem.

Now, here is the thing that I'm going to do that is going to be contentious because I know a lot of functional programmers that would absolutely just use filter for this. But what I'm going to do instead is I'm going to use a nested for loop. And the reason why I'm gonna use a nested for loop instead of a filter is because it's more efficient.

Because in a nested for loop I can break, so I can say, go through all of these until you find a match and then stop. But with a .filter, I can't do that. So what I'm gonna do to keep it efficient, typically I would use filters and things.

But because we're potentially working with a large amount of information, and I'm going to be a little bit performance budget conscious here. So I'm gonna say let result, and then return the results. And I'm gonna say, for (let i = 0: i < this.fooddata.length). Never thought you'd be watching people just code up for loops.

[LAUGH] So here we got the this for loop. And then, so remember, we have that first level of the stores, and then we also have the second level of those menu items. So I need to go through this and say .menu.length, and then I'm gonna change these all to J.

Because that's just kind of industry standard, when we loop through twice. We're looping through first those store pieces, then we're doing the .menu.length. Then we say if all of this, the menu item =, in other words, here. Then we're going to say that the result is J, and then we're gonna say break.

And that's gonna allow us to stop that process when we're done. So now I'm going to output currentItem and see if we did that correctly. Why is it .2, that seems weird. this.fooddata, I'm assigning instead of checking a quality. All right, cool, so now I go back, and cool, we have the description, we have the items, we have the image, we have the ID.

We have the price and the add ons, excellent.

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