Frameworks
Vue

Vue

New projects

The fast way to get started with Triplit is to use Create Triplit App which will scaffold a Vue application with Triplit. Choose Vue when prompted for the frontend framework.

npm create triplit-app@latest my-app

Existing projects

If have an existing Vue project, you install the hooks provided by @triplit/vue:

npm i @triplit/vue

useQuery

The useQuery hook subscribes to the provided query inside your Vue component and will automatically unsubscribe from the query when the component unmounts.

The result of the hook is an object with the following properties:

  • results: An array of entities that satisfy the query.
  • fetching: A boolean that will be true initially, and then turn false when either the local fetch returns cached results or if there were no cached results and the remote fetch has completed.
  • fetchingLocal: A boolean indicating whether the query is currently fetching from the local cache.
  • fetchingRemote: A boolean indicating whether the query is currently fetching from the server.
  • error: An error object if the query failed to fetch.
  • updateQuery: A function that can be called to update the query.
App.vue
<script setup lang="ts">
import { useQuery } from '@triplit/vue';
import Todo from './components/Todo.vue';
import { computed } from 'vue';
import { client } from './triplit';
 
const {
  fetching,
  results: todos,
  error,
} = useQuery(client, client.query('todos'));
</script>
 
<template>
  <div>
    <h1>Todos</h1>
    <span v-if="fetching">Loading...</span>
    <span v-else-if="error">Error: {{ error.message }}</span>
    <ul v-else-if="todos">
      <Todo
        v-for="todo in todos"
        :key="todo.id"
        :id="todo.id"
        :text="todo.text"
        :completed="todo.completed"
      />
    </ul>
  </div>
</template>

useConnectionStatus

The useConnectionStatus hook subscribes to changes to the connection status of the client and will automatically unsubscribe when the component unmounts.

ConnectionStatus.vue
<script setup lang="ts">
import { useConnectionStatus } from '@triplit/vue';
import { client } from './triplit';
 
const { connectionStatus } = useConnectionStatus(client);
</script>
<template>
  <div>
    <span v-if="connectionStatus === 'OPEN'">🟢</span>
    <span v-else-if="connectionStatus === 'CLOSED'">🔴</span>
    <span v-else>🟡</span>
  </div>
</template>