Vue使用epubjs电子书

npmjs: https://www.npmjs.com/package/epubjs

在线电子书转换器

安装:

javascript 复制代码
npm i epubjs

简单封装:

src/hooks/

javascript 复制代码
import Epub from "epubjs";
import type { Book, Rendition } from 'epubjs'
import type { BookOptions } from 'epubjs/types/book'
import type { RenditionOptions } from 'epubjs/types/rendition'

export function useEpub() {
  let book: Book
  let rendition: Rendition

  function createBook(urlOrData?: string | ArrayBuffer, options?: BookOptions) {
    if(!urlOrData) {
      book = Epub(options)
    } else {
      book = Epub(urlOrData, options)
    }
    return book
  }

  function render(element: Element | string, options?: RenditionOptions) {
    if(!book) return
    if(typeof element === 'string') {
      rendition = book.renderTo(element, options)
    } else {
      rendition = book.renderTo(element, options)
    }
    return rendition
  }

  function display() {
    if(!rendition) return
    return rendition.display()
  }

  function getBook() {
    return book
  }

  function getRendition() {
    return rendition
  }

  return { createBook, render, display, getBook, getRendition }
}

使用:

javascript 复制代码
<template>
  <div class="main">
    <div id="epub"></div>
    <div class="btn">
      <button @click="pre">pre</button>
      <button @click="next">next</button>
    </div>
  </div>
</template>
<script setup lang="ts">
import { useEpub } from '../hooks'
import { onMounted } from 'vue'

const { createBook, render, display, getRendition } = useEpub()

onMounted(() => {
  createBook('static/example.epub')
  render('epub', { width: '100%', height: '100%' })
  display()
})

// 上一页
const pre = async () => {
  await getRendition().prev()
}

// 下一页
const next = async () => {
  await getRendition().next()
}
</script>
<style scoped>
.main {
  width: 100%;
  height: 100%;
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
}

.main > #epub {
  width: 500px;
  height: 500px;
  border: 1px dashed red;
  box-sizing: border-box;
}
.main .btn {
  display: flex;
  justify-content: space-between;
  margin-top: 5px;
}
.main .btn button {
  padding: 7px 15px;
  margin-left: 20px;
}
</style>

这个示例电子书地址:https://example-files.online-convert.com/ebook/epub/example.epub

直接下载下来,放在public/static下。

这篇只是简单写一下使用,还没有正式用到

更多可以参考另一位博主的文章:https://blog.csdn.net/qq_42484429/article/details/93158645?ops_request_misc=&request_id=&biz_id=102&utm_term=vue%E4%BD%BF%E7%94%A8epubjs&utm_medium=distribute.pc_search_result.none-task-blog-2~all~sobaiduweb~default-2-93158645.142^v96^pc_search_result_base9&spm=1018.2226.3001.4187

写的相对更详细一些,也可以看着源码调用对应方法来实现对应功能。

相关推荐
墨林陌15 分钟前
AI 热点日报(2026-11-09):苹果代理式Siri曝光,Anthropic推进IPO,编码智能体实测翻车
前端
vipxieliang16 分钟前
JavaScript 迭代器与生成器:控制异步与惰性求值
前端·javascript
Hilaku25 分钟前
Event Loop 这篇你看完就不用再看别的了!
前端·javascript·程序员
恋猫de小郭1 小时前
Flutter 下 AOT 一个你怎么都想不出来的 Bug ,JIT 正常的计算,在 AOT 居然算错了
android·前端·flutter
晓得迷路了1 小时前
栗子前端技术周刊第 153 期 - Vitest 5.1、Remix v3.2、Node.js 27 Alpha...
前端·javascript·vite
系统集成-鹰翔2 小时前
ERP单据已作废为什么商品仍不能删除,引用检查与软删除怎么查
javascript·经验分享·后端
Moment2 小时前
Harness Engineering 为什么是 AI Coding 的工程底座 ❓❓❓
前端·后端·面试
头发还在的女程序员2 小时前
基于 Spring Boot + Vue 3 + uni-app 的灵活用工系统:四端架构与业务流程设计
vue.js·spring boot·uni-app·灵活用工·灵工系统
Penge6662 小时前
《Go 后端开发者学 React:Hook》
javascript