跳到正文

Nuxt ​

@paperpic/nuxt 是适用于 Nuxt 4 的模块。它注册 @paperpic/vue 的 PaperPic 组件,并把该组件的样式表添加到你的应用中,这样模板无需任何导入就能使用查看器。

安装 ​

sh
npm install @paperpic/nuxt
sh
pnpm add @paperpic/nuxt
sh
yarn add @paperpic/nuxt
sh
bun add @paperpic/nuxt

然后把模块添加到 nuxt.config.ts:

ts
export default defineNuxtConfig({
  modules: ['@paperpic/nuxt'],
})

@paperpic/vue 会随模块一起安装,你不需要自己安装。

用法 ​

vue
<script setup lang="ts">
import type { ExportedImage } from '@paperpic/nuxt'

function upload({ blob }: ExportedImage) {
  // 把编辑后的图片发送到你的服务器
}
</script>

<template>
  <div style="height: 480px">
    <PaperPic src="/receipt.jpg" alt="A receipt" @apply="upload" />
  </div>
</template>

组件会填满其父元素,因此请为父元素设置高度。它就是 @paperpic/vue 导出的那个组件,因此它的 props、事件和方法请参阅 Vue 页面。

本文档中的 Vue 示例在 Nuxt 中同样可用:删掉从 @paperpic/vue 导入组件和 style.css 的那几行,并改为从 @paperpic/nuxt 导入类型。测试也要稍作调整,见测试页面。

懒加载 ​

Nuxt 还会把该组件注册为 <LazyPaperPic>。它只在首次渲染时才下载查看器的代码,例如在用户打开对话框时,因此不立即显示查看器的页面会加载更少的代码。样式表是应用的一部分,所以查看器出现时它已经就绪。

类型 ​

@paperpic/nuxt 导出了 @paperpic/vue 的所有类型,因此请从它导入类型,例如 import type { PaperPicInstance } from '@paperpic/nuxt'。类型页面列出了每个类型包含的内容。

服务端渲染 ​

组件在服务器上渲染一个空元素,并在挂载时于浏览器中创建查看器,因此无需用 <ClientOnly> 包裹。

选项 ​

该模块目前还没有自己的选项。和在 Vue 中一样,每个查看器都通过组件的 props 进行配置。

基于 MIT 许可证发布。