• API References

@prismicio/vue - v5

Overview

@prismicio/vue is the official Prismic package for creating Vue.js-based websites.

With this package, you can:

This page documents all APIs provided by @prismicio/vue.

Install

@prismicio/vue is automatically installed when bootstrapping a Nuxt project with @slicemachine/init.

npx @slicemachine/init@latest

It can also be manually installed in any Vue.js project:

npm install --save @prismicio/vue

Register the Vue plugin

@prismicio/vue provides a Vue.js plugin that injects @prismicio/client and Vue components into your project.

  • Configure your plugin with createPrismic

    createPrismic configures a Vue.js plugin with your Prismic repository.

    prismic.ts
    import { createPrismic } from "@prismicio/vue";
    
    const prismic = createPrismic({
      endpoint: "example-prismic-repo",
    });
    
    export default prismic;
  • Register the plugin in your Vue app

    The plugin needs to be registered in your Vue app.

    main.ts
    import { createApp } from "vue";
    import App from "./App.vue";
    import prismic from "./prismic";
    
    createApp(App).use(prismic).mount("#app");

Access Prismic client and helpers

The plugin injects @prismicio/client’s client and helpers into your Vue app. They can be accessed with usePrismic in your component setup and $prismic in your template sections.

App.vue
<script setup lang="ts">
import { usePrismic } from "@prismicio/vue";

const {
  options,
  client,
  filter,
  cookie,
  // All @prismicio/client helpers are available.
  asText,
  asHTML,
} = usePrismic();
</script>

<template>
  <div>
    {{ $prismic.options }}

    {{ $prismic.client }}
    {{ $prismic.filter }}
    {{ $prismic.cookie }}

    <!-- All @prismicio/client helpers are available. -->
    {{ $prismic.asText }}
    {{ $prismic.asHTML }}
  </div>
</template>

API

createPrismic

Creates a Vue.js plugin with your Prismic endpoint and additional options.

createPrismic({
  endpoint: "example-prismic-repo",
});
property
Description
Default
endpoint
string

A Prismic repository name (recommended) or full endpoint.

clientConfig
optional
object

Options for @prismicio/client used to create a client. Only one of clientConfig or client can be used.

client
optional
PrismicClient

An explicit @prismicio/client’s client for the plugin to use directly instead of creating one. Only one of clientConfig or client can be used.

linkResolver
optional
(field: ContentRelationshipField) => string | null | undefined

A link resolver function used to determine a URL from a link field or document. If it returns a value, it takes priority over a route resolver. Prefer using only a route resolver with the plugin’s clientConfig.routes option.

injectComponents
optional
boolean

Whether to inject Prismic components globally into the Vue app.

true
components
optional
object

Options to configure @prismicio/vue’s components.

usePrismic

Returns the @prismicio/client’s client and helpers injected by @prismicio/vue.

const prismic = usePrismic();

Components

<PrismicImage>

Displays an optimized image from an image field.

<PrismicImage :field="slice.primary.image" />
prop
Description
Default
field
ImageField
An image field to display.
imgixParams
optional
object

An object of imgix URL parameters. See an example.

alt
optional
""
Declare an image as decorative.
fallbackAlt
optional
""

Declare an image as decorative if the image field does not have alt text.

widths
optional
number[] | "thumbnails" | "defaults"

Widths used to build a srcset value for the image field. - "thumbnails" uses the image field’s thumbnails. - "defaults" uses the plugin’s components.imageWidthSrcSetDefaults value. Only one of widths or pixelDensities can be used.

"defaults"
pixelDensities
optional
number[] | "defaults"

Widths used to build a srcset value for the image field. - "defaults" uses the plugin’s components.imagePixelDensitySrcSetDefaults value. Only one of widths or pixelDensities can be used.

Displays a link from a link field or a document.

<PrismicLink :field="slice.primary.link" />
prop
Description
Default
field
LinkField

A link field to link. Only one of field or document can be used.

document
PrismicDocument

A Prismic document to link. Only one of field or document can be used.

linkResolver
optional
(field: ContentRelationshipField) => string | null | undefined

A link resolver function used to determine a URL from the field or document prop. If it returns a value, it takes priority over a route resolver. Prefer using only a route resolver with the plugin’s clientConfig.routes option.

rel
optional
undefined | string | ((metadata) => string | undefined)

A rel attribute’s value to apply on the link. A function can be provided to use the link’s metadata to determine the rel value. Use undefined to remove the rel attribute.

"noreferrer" if the link is external.
internalComponent
optional
Component | string

The component used to render internal links.

RouterLink || NuxtLink
externalComponent
optional
Component | string

The component used to render external links.

"a" || NuxtLink

<PrismicText>

Displays plain text from a rich text field.

<PrismicText :field="slice.primary.text" />
prop
Description
Default
field
RichTextField
A rich text field to render.
fallback
optional
string

Rendered if the rich text field is empty.

separator
optional
string

Separator used between text blocks.

" "
wrapper
optional
Component | string

An HTML tag name or a component used to wrap the output. The output is not wrapped by default.

<PrismicRichText>

Displays formatted text from a rich text field.

<PrismicRichText :field="slice.primary.text" />
prop
Description
Default
field
RichTextField
A rich text field to render.
fallback
optional
Component

Rendered if the rich text field is empty.

components
optional
VueRichTextSerializer

A map of rich text block types to Vue components. See an example.

Standard HTML elements (e.g. heading1 becomes a <h1>).

linkResolver
optional
(field: ContentRelationshipField) => string | null | undefined

A link resolver function used to determine a URL from the field or document prop. If it returns a value, it takes priority over a route resolver. Prefer using a route resolver with the plugin’s clientConfig.routes option instead.

wrapper
optional
Component | string

An HTML tag name or a component used to wrap the output. The output is not wrapped by default.

<PrismicEmbed>

Displays HTML from an embed field.

<PrismicEmbed :field="slice.primary.embed" />
prop
Description
Default
field
EmbedField
An embed field to render.
wrapper
optional
Component | string

An HTML tag name or a component used to wrap the output.

"div"

<PrismicTable>

Displays a formatted table from a table field.

<PrismicTable :field="slice.primary.my_table_field" />
prop
Description
Default
field
TableField
A table field to render.
fallback
optional
Component

Rendered if the table field is empty.

components
optional
VueTableComponents & VueRichTextSerializer

A map of table elements and and rich text block types to Vue components. The available table elements are table, thead, tbody, tr, th, and td. The available rich text block types are paragraph, em, strong, and hyperlink. See an example.

<SliceZone>

Renders slices from a slice zone.

<SliceZone :slices="doc.data.slices" :components="components" />
prop
Description
Default
slices
SliceZone
A slice zone to render.
components
object

A map of slice types to Vue components.

defaultComponent
optional
Component

Rendered if a slice does not have a component mapping.

context
optional
any

Arbitrary data made available to all slice components.

wrapper
optional
Component | string

An HTML tag name or a component used to wrap the output. The output is not wrapped by default.

Prop helpers

getSliceComponentProps

Returns prop types for a slice component. Components rendered by <SliceZone> should use this helper.

defineProps(getSliceComponentProps());

getRichTextComponentProps

Returns prop types for a rich text field block component. Components rendered by <PrismicRichText> should use this helper.

defineProps(getRichTextComponentProps(type));
prop
Description
Default
type
optional
string

The component’s rich text node type.

getTableComponentProps

Returns prop types for a table field component. Components rendered by <PrismicText> should use this helper.

defineProps(getTableComponentProps.table());
defineProps(getTableComponentProps.thead());
defineProps(getTableComponentProps.tbody());
defineProps(getTableComponentProps.tr());
defineProps(getTableComponentProps.th());
defineProps(getTableComponentProps.td());

Upgrading from v4

Follow these steps to upgrade an existing project to @prismicio/vue v5.