In this post, we’ll be using an ecommerce store demo I built and deployed to Netlify to show how we can make dynamic routes for incoming data. It’s a fairly common use-case: you get data from an API, and you either don’t know exactly what that data might be, there’s a lot of it, or it might change. Luckily for us, Nuxt makes the process of creating dynamic routing very seamless. In the last post, we showed how to set up stripe payments with Netlify Functions, which allow us to create serverless lambda functions with ease. We’ll use this same application to show another Nuxt-specific functionality as well. Let’s get started!
Creating the pageIn this case, we’ve got some dummy data for the store that I created in mockaroo and am storing in the static folder. Typically you’ll use fetch or axios and an action in the Vuex store to gather that data. Either way, we store the data with Vuex in
It’s important to mention that in Nuxt, all we have to do to set up routing in the application is create a In order to create dynamic routing, we will make a directory to house those pages. In this case, I made a directory called In that directory, I’ll create a page with an underscore, and the unique indicator I want to use per page to create the routes. If we look at the data I have in my cart, it looks like this:
You can see that the ID for each entry is unique, so that’s a good candidate for something to use, we’ll call the page:
Now, we can store the id of the particular page in our data by using the route params:
For the entry from above, our data if we looked in devtools would be:
We can now use this to retrieve all of the other information for this entry from the store. I’ll use
And we’re filtering the Let the Nuxt config knowIf we were building an app using This command will expect a function that will return a promise that resolves in an array that will look like this:
To create this, at the top of the file we’ll bring in the data from the static directory, and create the function:
We’ll then call the function within our config:
If you’re gathering your data from an API with axios instead (which is more common), it would look more like this:
And with that, we’re completely done with the dynamic routing! If you shut down and restart the server, you’ll see the dynamic routes per product in action! For the last bit of this post, we’ll keep going, showing how the rest of the page was made and how we’re adding items to our cart, since that might be something you want to learn, too. Populate the pageNow we can populate the page with whatever information we want to show, with whatever formatting we would like, as we have access to it all with the product computed property:
In our case, we’ll also want to add items to the cart that’s in the store. We’ll add the ability to add and remove items (while not letting the decrease count dip below zero
In our methods on that component, we’ll add the item plus a new field, the quantity, to an array that we’ll pass as the payload to mutation in the store.
In the Vuex store, we’ll check if the item already exists. If it does, we’ll just increase the quantity. If not, we’ll add the whole item with quantity to the cart array.
We can now use a getter in the store to calculate the total, which is what we’ll eventually pass to our Stripe serverless function (the other post picks up from here). We’ll use a reduce for this as reduce is very good at retrieving one value from many. (I wrote up more details on how reduce works here).
And there you have it! We’ve set up individual product pages, and Nuxt generates all of our individual routes for us at build time. You’d be Nuxt not to try it yourself. ? The post Creating Dynamic Routes in a Nuxt Application appeared first on CSS-Tricks. from CSS-Tricks https://css-tricks.com/creating-dynamic-routes-in-a-nuxt-application/ Creating Dynamic Routes in a Nuxt Application See more on: Instant Web Site Tools Blog from https://www.instant-web-site-tools.com/2019/07/30/creating-dynamic-routes-in-a-nuxt-application/
0 Comments
Scott Jehl:
Don't just up and do this to all your stylesheets though, otherwise, you'll get a pretty nasty "Flash of Unstyled Content" (FOUC) as the page loads. You need to pair the technique with a way to ship critical CSS. Do that though, and like Scott's opening sentence said, it's quite impactful. Interesting side story... on our Pen Editor page over at CodePen, we had a FOUC problem:
What makes it weird is that we load our CSS in Direct Link to Article — Permalink The post The Simplest Way to Load CSS Asynchronously appeared first on CSS-Tricks. from CSS-Tricks https://www.filamentgroup.com/lab/load-css-simpler/ The Simplest Way to Load CSS Asynchronously Find more on: Instant Web Site Tools from https://www.instant-web-site-tools.com/2019/07/30/the-simplest-way-to-load-css-asynchronously/ React has a built-in hook called useEffect. Hooks are used in function components. The
In a totally isolated example like that, it's likely the The trick is that
|