Add Routing to Your Vue 3 Application With Vue Router
lessonDuration 3m 8s
In our SPA we need to be able to navigate between pages without triggering a refresh. We accomplish this by setting up routes with Vue Router.
Browse
Find courses, lessons, articles, talks, and podcasts.
7,176 resources
Showing 1,465–1,488 · Page 62 of 299
lessonDuration 3m 8s
In our SPA we need to be able to navigate between pages without triggering a refresh. We accomplish this by setting up routes with Vue Router.
lessonDuration 3m 14s
A Vue composable is kind of like a React hook. They allow you to encapsulate your logic and reuse it throughout your applications. In this lesson, we'll learn how to create a new composable, and use it with both a setup script and the object API.
lessonDuration 1m 22s
In order to display dynamic pages we need to create dynamic routes. With Vue Router we do this by writing the route path like this `/craft/:id` . The `:` prefix indicates the dynamic part of the route.
lessonDuration 51s
Template literals give you a convenient way to handle routing with dynamic data. In this lesson, we use a template literal on `router-link`'s `:to` property.
lessonDuration 3m 39s
We can also make our GraphQL queries dynamic by using query parameters. In this lesson, we create a query for the CraftView page, which requires an ID that we pass through query parameters. In our Vue app, we can dynamically pass the id into the second argument of the `useQuery` composable. We get the id from the route using the `useRoute` composable.
lessonDuration 2m 32s
Vue gives you the convenient `v-if` property to conditionally render parts of your UI. In this lesson, we will use a `ref` initially set to false, and create a button that toggles it to true. Then we'll create a `div` with the `class` of `"modal"` and a `v-if` property that conditionally renders based on our `ref`
lessonDuration 2m 16s
Sometimes we need a child component to change the state of its parent component. We can do that by emitting an event using the globally defined function, `defineEmits`, and listening for it where the component is used.
lessonDuration 1m 20s
Since we are preventing a page refresh on form submit, we don't see that our data is updated in the UI until we manually refresh the page. We can display the correct data without a refresh by using the `refetch` method from the composable `useQuery`. In this lesson, we will emit an `updated` event that triggers a `handleRefresh` method, inside of which is a call to `refetch`.
lessonDuration 3m 20s
What do we do with optimistic updates when things don't go the way we planned? Luckily, the Apollo client is smart and corrects the data, but it might not be clear to the user that their mutation didn't go through. We can have our component emit and error event and that error can be handled by the parent component. In our case, we'll use a `v-if` to display a message that informs the user their request didn't go through and the updated values were reverted.
lessonDuration 2m 2s
Up until this point we have been using `ref` for our state. But we need to create a form with multiple fields so we need to use a more complex data structure. `reactive`, unlike `ref`, does a deep comparison of the data, so we can use an array or object with it. Then, to update our reactive data, we use the `v-model` property on our inputs. This keeps our data in sync with the current input value.
lessonDuration 1m 34s
Now we need to actually update our data. This is done in Graphql with "mutations". In this lesson, we design a mutation in GraphiQL. Make sure to wrap your mutation in `mutation() {}`. You will also pass your variables as arguments to `mutation` and declare their type before you can use them in your mutations.
lessonDuration 2m 8s
The form can update state locally but changes don't persist. We need to take the mutation we wrote in GraphiQL and use it in our Vue app. Using the `useMutation` composable, we pass it our `gql` and an arrow function that returns the variables. The variables are assigned to our `updateFields` object's properties. After destructuring an update function off of `useMutation`, we define a `handleSubmit` function and call our update function inside. Then, we emit a close event and prevent the default behavior of the form.
lessonDuration 3m 27s
We have a thousand crafts but our app currently only displays twenty. We can use the `fetchMore` method off of the `useQuery` composable to run our query again with new variables. We modify our query to take an offset variable. And then in our app, when we run `fetchMore` and concat the query results to our existing array of crafts.
course
**Get started building dynamic apps with GraphQL and Vue3.** As your app scales, so does the complexity of managing your routes and API endpoints. It gets tiresome. But this aspect of web development can be streamlined considerably! GraphQL is a powerful tool for querying and mutating your data. In this course, we are going to create a Vue3 app that leverages GraphQL to create a dynamic front end. But what does GraphQL *do*? Its power comes from giving you a *single* endpoint that you make all your queries to. Instead of making a request to a specific endpoint and getting a fixed response, you can write flexible queries to a single endpoint.  Vue3 has some great new features that we will be exploring, such as the setup script syntax. But you won't just be exploring Vue3's new features, we will be building a dynamic front end with an index, view pages generated from data, and forms. To make our frontend dynamic, we will be powering it with GraphQL. We start with the basics by creating queries but we move on to more advanced topics such as query params, mutations, and pagination. To actually use GraphQL in our Vue app we will be using the Vue Apollo Client. Which gives us convenient methods for making queries, getting the data back into the client, and handling errors. ## Gain These Skills - Add GraphQL to a Vue 3 project - Query and mutate data with GraphQL in Vue 3 - Create data-driven views with Apollo client - Handle routing within a Vue 3 project - Implement optimistic updates and infinite scroll ## Prerequisites Some entry-level knowledge on Vue and GraphQL is recommended, but this course is beginner-friendly enough to where it isn't a strict requirement. If you want to build a deeper foundation for both Vue and GraphQL we have a couple of awesome courses that you can hack through. For Vue, check out **[The Beginner's Guide to Vue 3](https://egghead.io/courses/the-beginner-s-guide-to-vue-3-1c46da8b),** which is also by Kevin. It's great for both newcomers to web dev and veterans alike. And for GraphQL, work through **[GraphQL Query Language](https://egghead.io/courses/graphql-query-language)** from Eve Porcello. Eve literally wrote the book on learning GraphQL and her course is similarly top quality. ## What's Next? Progressive Web Apps are a more advanced topic, and converting an existing Single Page **Vue** App into one makes for an excellent follow-up project. **->** [Offline-First Progressive Web Apps (PWA) in Vue.js](https://egghead.io/courses/offline-first-progressive-web-apps-pwa-in-vue-js) If you want to take your **GraphQL** game to the next level, then becoming an expert in designing GraphQL schemas is the way to go. **->** [Designing GraphQL Schemas](https://egghead.io/courses/designing-graphql-schemas-99db) And I know the following recommendation is a React course but *it really is that good*. You will learn some **advanced** techniques that will be applicable to Vue or any other Apollo compatible framework that you work with. **->** [Manage State in React Apps with Apollo Client and GraphQL](https://egghead.io/courses/manage-state-in-react-apps-with-apollo-client-and-graphql-a45b3b89)
lessonDuration 3m 35s
Having a hardcoded source of data isn't going to work out in the short or long term. We need something a little more dynamic. That's why we'll be learning how to create a **DynamoDB table** to store our data instead! In this lesson, we will create a table in our CDK app, and grant our lambda function read access to the table.
lessonDuration 2m 7s
The default key that is generated for your AppSync API expires in only 7 days. It would be a big pain in the fingers to update your app every 7 days! Instead, you can use AppSync to create and configure your own API key, where you can set whatever expiration date you want.
lessonDuration 4m 42s
There are only a few steps you have to take to create a live GraphQL API with AWS CDK's AppSync package. First, define a GraphQL schema. This is where you define the data you want to query and create query and mutation functions. Then, AppSync is used to create a new GraphQL API, passing in the schema you just created. When you deploy, an API key will be generated. You will need this key to interact with the API.
lessonDuration 1m 18s
Time to deploy! Learn how to use the `npm run watch` command to watch for changes and compile TypeScript to JavaScript. But, the main thing you'll learn is how to use the `cdk deploy` command to deploy your app to AWS and then navigate to your app through the AWS web console.
lessonDuration 1m 14sFree
You can create an AWS CDK project entirely on the command. In this lesson, you will learn how to install the AWS CLI to view all of you existing S3 bucks and to initialize a CDK app with TypeScript. You can find the CLI documentation here at [Configuring the AWS CLI](https://docs.aws.amazon.com/cli/latest/userguide/cli-chap-configure.html)
lessonDuration 4m 27s
Without data, you can't query for data 🤔 You can add data source resolvers to your GraphQL API in the AWS console, but in this lesson, we'll be doing it all in our CDK app. This is in adherence with the infrastructure as code best practice. We'll create a lambda function entirely within our CDK app. We'll create a data source with our AppSync GraphQL API and add our lambda function as the handler. Then we have to make sure that data-source is invoked when there is a query to `listBooks`
lessonDuration 2m 45s
You may notice that sometimes when you make a query or mutation after not using your app for a while, it will be extra slow. What gives? There are a couple of things happening. First, a lambda function goes through a "cold-start" after being run for the first time in a while. AWS is downloading the code and spinning up an environment. Second, if you are using the default settings, your function doesn't have much memory to work with. You can specify the amount of memory your function has available in its configuration.
lessonDuration 6m 3s
Right now our database is read-only. Let's fix that! In GraphQL you make changes to your database using what are called *mutations*. We can define a mutation in our schema and then create a new lambda function that handles writing to our DynamoDB table.
lessonDuration 2m 1s
It would quickly get out of hand if we had to create TypeScript types for every type in our GraphQL schema. Luckily there are a couple packages from `graphql-codegen` that we can use to make our lives easier. We'll create a `codegen.yml` file where we'll specify our GraphQL schema and choose the location of the types file we want to generate based on the schema. Then we can create a script in our package.json that runs the codegen. For more on using TypeScript with AppSync Lambda Resolvers, check out Benoît Bouré blog post: [How to use TypeScript with AppSync Lambda Resolvers](https://benoitboure.com/how-to-use-typescript-with-appsync-lambda-resolvers)
lessonDuration 4m 48s
Now we have to set up our Lambda function to actually read data from our DynamoDB table. The hard coded data in our handler function is getting deleted and being replaced with code that scans our table and returns its contents. To make this happen we'll be using `aws-sdk` to get access to `DynamoDB.DocumentClient` which gives us a simple API for interacting with our tables.