Vue Js Guide

Posted on  by admin

You’re browsing the documentation for v2.x and earlier. For v3.x, click here.

Every Vue application starts by creating a new Vue instance with the Vue function:. Although not strictly associated with the MVVM pattern, Vue’s design was partly inspired by it.


As a convention, we often use the variable vm (short for ViewModel) to refer to our Vue instance.

# schema.graphqltypeAuthor {id: Int!firstName: String!lastName: String!posts(findTitle: String): [Post]}typePost {id: Int!title: String!author: Author}typeQuery {post(id: ID!): Post}

Data and Methods

When you create a Vue instance, you pass in an options object.

import { useQuery } from'@urql/vue'interfacePostQueryVariables {id: string}exportdefault {setup() {constresult = useQuery({query:` query ($id: ID!) { post(id: $id) { id title author { id firstName lastName } } } `,// we need to type variables manuallyvariables: { id:1 } asPostQueryVariables })return {from,data:result.data } }}

The majority of this guide describes how you can use these options to create your desired behavior.

For reference, you can also browse the full list of options in the API reference. A Vue application consists of a root Vue instance created with new Vue, optionally organized into a tree of nested, reusable components.


For example, a todo app’s component tree might look like this:.

We’ll talk about the component system in detail later. For now, just know that all Vue components are also Vue instances, and so accept the same options object (except for a few root-specific options).

{"scripts": {"generate": "graphql-codegen" }}

When a Vue instance is created, it adds all the properties found in its data object to Vue’s reactivity system.

pnpm generate

When the values of those properties change, the view will “react”, updating to match the new values.

import { usePostQuery } from'../generated/graphql'exportdefault {setup() {// `variables` option and `result` are typed!constresult = usePostQuery({variables: { id:1 } })return {from,data:result.data } }}

When this data changes, the view will re-render. It should be noted that properties in data are only reactive if they existed when the instance was created.

That means if you add a new property, like:. Then changes to b will not trigger any view updates.

Instance Lifecycle Hooks

If you know you’ll need a property later, but it starts out empty or non-existent, you’ll need to set some initial value.

interfacePostQueryVariables {id: string}exportdefault {apollo: {post: {query:gql` query ($id: ID!) { post(id: $id) { id title author { id firstName lastName } } } `,variables: {id:1 } asPostQueryVariables } },data() {return {post:undefined } }}

The only exception to this being the use of Object.freeze(), which prevents existing properties from being changed, which also means the reactivity system can’t track changes.

In addition to data properties, Vue instances expose a number of useful instance properties and methods. These are prefixed with $ to differentiate them from user-defined properties.


In the future, you can consult the API reference for a full list of instance properties and methods.

Each Vue instance goes through a series of initialization steps when it’s created - for example, it needs to set up data observation, compile the template, mount the instance to the DOM, and update the DOM when data changes.

{"scripts": {"generate": "graphql-codegen" }}

Along the way, it also runs functions called lifecycle hooks, giving users the opportunity to add their own code at specific stages.

pnpm generate

For example, the created hook can be used to run code after an instance is created:.

import { usePostQuery } from'../generated/graphql'exportdefault {apollo:usePostQuery({// `variables` is properly typed!variables: {id:1 } }),data() {return {post:undefined } }}

There are also other hooks which will be called at different stages of the instance’s lifecycle, such as mounted, updated, and destroyed.

All lifecycle hooks are called with their this context pointing to the Vue instance invoking it.