@prismicio/vue - v5
Overview
@prismicio/vue is the official Prismic package for creating Vue.js-based websites.
With this package, you can:
Query Prismic content with a dedicated client.
Display slice zones with custom components.
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@latestIt can also be manually installed in any Vue.js project:
npm install --save @prismicio/vueRegister the Vue plugin
@prismicio/vue provides a Vue.js plugin that injects @prismicio/client and Vue components into your project.
Configure your plugin with
createPrismiccreatePrismicconfigures a Vue.js plugin with your Prismic repository.prismic.tsimport { 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.tsimport { 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.
<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",
});endpointstringA Prismic repository name (recommended) or full endpoint.
clientConfigobjectOptions for @prismicio/client
used to create a client. Only one of clientConfig or client can be
used.
clientPrismicClientAn 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(field: ContentRelationshipField) => string | null | undefinedA 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.
injectComponentsbooleanWhether to inject Prismic components globally into the Vue app.
truecomponentsobjectOptions to configure @prismicio/vue’s components.
linkRelstring | functionA rel attribute’s value to apply on links rendered by PrismicLink.
A function can be provided to use the link’s metadata to determine the
rel value.
({isExternal}) => isExternal ? "noreferrer" : undefined
linkInternalComponentComponent | stringA component used by PrismicLink to render internal links.
RouterLink || NuxtLinklinkExternalComponentComponent | stringA component used by PrismicLink to render external links.
"a" || NuxtLinkimageWidthSrcSetDefaultsnumber[]Default widths to use when rendering an image with PrismicImage’s
widths="defaults" option.
[640, 828, 1200, 2048, 3840]imagePixelDensitySrcSetDefaultsnumber[]Default pixel densities to use when rendering an image with
PrismicImage’s pixel-densities="defaults" option.
[1, 2, 3]richTextComponentsVueRichTextSerializerA map of rich text block types to Vue components to use with
PrismicRichText.
sliceZoneDefaultComponentComponentRendered if a component mapping from the SliceZone’s components
prop cannot be found.
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" />fieldImageFieldalt""fallbackAlt""Declare an image as decorative if the image field does not have alt text.
widthsnumber[] | "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"pixelDensitiesnumber[] | "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.
<PrismicLink>
Displays a link from a link field or a document.
<PrismicLink :field="slice.primary.link" />fieldLinkFieldA link field to link. Only one of field or document can be used.
documentPrismicDocumentA Prismic document to link. Only one of field or document can be used.
linkResolver(field: ContentRelationshipField) => string | null | undefinedA 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.
relundefined | 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.internalComponentComponent | stringThe component used to render internal links.
RouterLink || NuxtLinkexternalComponentComponent | stringThe component used to render external links.
"a" || NuxtLink<PrismicText>
Displays plain text from a rich text field.
<PrismicText :field="slice.primary.text" />fieldRichTextFieldfallbackstringRendered if the rich text field is empty.
separatorstringSeparator used between text blocks.
" "wrapperComponent | stringAn HTML tag name or a component used to wrap the output. The output is not wrapped by default. Prefer wrapping the component with an HTML element instead.
<PrismicRichText>
Displays formatted text from a rich text field.
<PrismicRichText :field="slice.primary.text" />fieldRichTextFieldfallbackComponentRendered if the rich text field is empty.
componentsVueRichTextSerializerA map of rich text block types to Vue components. The available heading types are heading1 to heading6. The available block types are paragraph, preformatted, list, oList, listItem, oListItem, image, and embed. The available inline types are strong, em, hyperlink, label, and span. See an example.
Standard HTML elements (e.g. heading1 becomes a <h1>).
linkResolver(field: ContentRelationshipField) => string | null | undefinedA 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.
wrapperComponent | stringAn HTML tag name or a component used to wrap the output. The output is not wrapped by default. Prefer wrapping the component with an HTML element instead.
<PrismicEmbed>
Displays HTML from an embed field.
<PrismicEmbed :field="slice.primary.embed" />fieldEmbedFieldwrapperComponent | stringAn 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" />fieldTableFieldfallbackComponentRendered if the table field is empty.
componentsVueTableComponents & VueRichTextSerializerA 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" />slicesSliceZonecomponentsobjectA map of slice types to Vue components.
sliceSliceThe slice object being displayed.
slicesSlice[]All slices in the slice zone.
indexnumberThe index of the slice in the slice zone.
contextunknownThe data passed to SliceZone’s context prop.
defaultComponentComponentRendered if a slice does not have a component mapping.
contextanyArbitrary data made available to all slice components.
wrapperComponent | stringAn HTML tag name or a component used to wrap the output. The output is not wrapped by default. Prefer wrapping the component with an HTML element instead.
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));typestringThe 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.
Install
@prismicio/vuev5@prismicio/vuev5 is automatically installed when using@nuxtjs/prismicv4.npm install --save @prismicio/vue@^5Provide your own
fetchpolyfillisomorphic-unfetchis no longer provided as a defaultfetchimplementation. If you still need afetchpolyfill, installisomorphic-unfetch:npm install --save isomorphic-unfetchThen, import and configure it with the plugin:
prismic.tsimport { createPrismic } from "@prismicio/vue"; import fetch from "isomorphic-unfetch"; const prismic = createPrismic({ endpoint: "your-repository-name", clientConfig: { fetch, }, }); export default prismic;Replace
htmlSerializerplugin optionUse the
richTextSerializeroption instead.prismic.tsimport { createPrismic } from "@prismicio/vue"; const prismic = createPrismic({ endpoint: "your-repository-name", htmlSerializer: serializer richTextSerializer: serializer }); export default prismic;Replace
components.imageComponentplugin optionUse your own image component instead with
@prismicio/client’s helpers.<MyImageComponent v-if="$prismic.isFilled(slice.primary.image)" :src="$prismic.asImageSrc(slice.primary.image)" />Replace
components.linkBlankTargetRelAttributeplugin optionUse the new
components.linkReloption.prismic.tsimport { createPrismic } from "@prismicio/vue"; const prismic = createPrismic({ endpoint: "your-repository-name", components: { linkBlankTargetRelAttribute: "noreferrer" linkRel: ({ isExternal }) => isExternal ? "noreferrer" : undefined } });Replace
<SliceZone>’sresolverpropUse the new
componentsprop instead.<SliceZone :slices="doc.data.slices" :resolver="resolver /* [!code --] */" :components="components /* [!code ++] */" />Replace
<PrismicImage>’simageComponentpropUse your own image component instead with
@prismicio/client’s helpers.<MyImageComponent v-if="$prismic.isFilled(slice.primary.image)" :src="$prismic.asImageSrc(slice.primary.image)" />Replace
<PrismicLink>’sfieldprop when linking documentsUse the new
documentprop instead.<PrismicLink :field="aboutUsDocument/* [!code --] */" :document="aboutUsDocument/* [!code ++] */" />Update
<PrismicLink>’srelattribute behaviorAll external links now default to
noreferrer. Previously only links with atarget="_blank"attribute had a default value.You can resume the previous behavior globally using the new
components.linkReloption.prismic.tsimport { createPrismic } from "@prismicio/vue"; const prismic = createPrismic({ endpoint: "your-repository-name", components: { linkRel: ({ isExternal }) => (isExternal ? "noreferrer" : undefined), }, });The function can also be passed directly to
<PrismicLink>’srelprop.<PrismicLink :field="slice.primary.link" :rel="({ isExternal }) => isExternal ? 'noreferrer' : undefined" />Update
<PrismicLink>’stargetattribute behaviorThe
targetprop is removed. Use the editor to control whether links should be opened in a new tab.Replace
<PrismicLink>’sblankTargetRelAttributepropUse the new
relprop.<PrismicLink :field="slice.primary.image" :blankTargetRelAttribute="'noreferrer' /* [!code --] */" :rel="({ isExternal }) => isExternal ? 'noreferrer' : undefined /* [!code ++] */" />Wrap
<PrismicText>output<PrismicText>output is no longer wrapped. Use your own wrapper tag.<p> <PrismicText :field="slice.primary.text" /> </p>Replace
<PrismicRichText>’shtmlSerializerpropMigrate to the new component-based serializer, or use the (now deprecated)
serializerprop during your migration instead.<PrismicRichText :field="slice.primary.text" :htmlSerializer="serializer /* [!code --] */" :serializer="serializer /* [!code ++] */" />Wrap
<PrismicRichText>output<PrismicRichText>output is no longer wrapped. Use your own wrapper tag.<div> <PrismicRichText :field="slice.primary.text" /> </div>Replace
usePrismicImage()Use
@prismicio/client’s helpers directly.import { usePrismicImage } from "@prismicio/vue"; const image = usePrismicImage(slice.primary.image); import { computed } from "vue"; import { usePrismic } from "@prismicio/vue"; const prismic = usePrismic(); const image = computed(() => { return prismic.asImageSrcSet(slice.primary.image); });Replace
usePrismicLink()Use
@prismicio/client’s helpers directly.import { usePrismicLink } from "@prismicio/vue"; const link = usePrismicLink(slice.primary.link); import { computed } from "vue"; import { usePrismic } from "@prismicio/vue"; const prismic = usePrismic(); const link = computed(() => { return prismic.asLinkAttrs(slice.primary.link); });Replace
usePrismicText()Use
@prismicio/client’s helpers directly.import { usePrismicText } from "@prismicio/vue"; const text = usePrismicText(slice.primary.text); import { computed } from "vue"; import { usePrismic } from "@prismicio/vue"; const prismic = usePrismic(); const text = computed(() => { return prismic.asText(slice.primary.text); });Replace
usePrismicRichText()Use
@prismicio/client’s helpers directly.import { usePrismicRichText } from "@prismicio/vue"; const html = usePrismicRichText(slice.primary.text); import { computed } from "vue"; import { usePrismic } from "@prismicio/vue"; const prismic = usePrismic(); const html = computed(() => { return prismic.asHTML(slice.primary.text); });Replace all query composables
Use a query library with a Prismic client, such as TanStack Query.
import { usePrismicDocumentByUID } from "@prismicio/vue"; const { data } = usePrismicDocumentByUID("page", "home"); import { usePrismic } from "@prismicio/vue"; import { useQuery } from "@tanstack/vue-query"; const prismic = usePrismic(); const { data } = useQuery({ queryKey: ["home"], queryFn: () => prismic.client.getByUID("page", "home"), });Refer to the following table for each hook’s replacement client method.
Hook Query method usePrismicDocuments()client.get()useFirstPrismicDocument()client.getFirst()useAllPrismicDocumentsDangerously()client.dangerouslyGetAll()usePrismicDocumentByID()client.getByID()usePrismicDocumentsByIDs()client.getByIDs()useAllPrismicDocumentsByIDs()client.getAllByIDs()usePrismicDocumentByUID()client.getByUID()usePrismicDocumentsByUIDs()client.getByUIDs()useAllPrismicDocumentsByUIDs()client.getAllByUIDs()useSinglePrismicDocument()client.getSingle()usePrismicDocumentsByType()client.getByType()useAllPrismicDocumentsByType()client.getAllByType()usePrismicDocumentsByTag()client.getByTag()useAllPrismicDocumentsByTag()client.getAllByTag()usePrismicDocumentsBySomeTags()client.getBySomeTags()useAllPrismicDocumentsBySomeTags()client.getAllBySomeTags()usePrismicDocumentsByEveryTag()client.getByEveryTag()useAllPrismicDocumentsByEveryTag()client.getAllByEveryTag()Replace
DefineComponentSliceComponentPropstypeUse the return type of
getSliceComponentPropsinstead.import { getSliceComponentProps } from "@prismicio/vue"; type DefineComponentSliceComponentProps = ReturnType< typeof getSliceComponentProps >;