vue2 中使用pdfjs 项目无法启动?

先安装的:

npm install pdfjs-dist --save

报错按提示安装的:

npm install --save babel-polyfill

也按网上说的把: main.js的第一行给了:

import "babel-polyfill"

项目启动报错:

 ERROR  Failed to compile with 1 error                                                                                                                                                            16:24:33

error in ./node_modules/pdfjs-dist/build/pdf.js

Module parse failed: Unexpected token (405:14)

You may need an appropriate loader to handle this file type, currently no loaders are configured to process this file. See https://webpack.js.org/concepts#loaders

| }

| function _isValidProtocol(url) {

> switch (url?.protocol) {

| case "http:":

| case "https:":

@ ./node_modules/cache-loader/dist/cjs.js??ref--12-0!./node_modules/babel-loader/lib!./node_modules/cache-loader/dist/cjs.js??ref--0-0!./node_modules/vue-loader/lib??vue-loader-options!./src/components/ImageMask/index.vue?vue&type=script&lang=js& 37:0-35 38:0-5 38:94-99 174:24-29

代码中这样使用的:

import { PDFJS } from 'pdfjs-dist'

PDFJS.GlobalWorkerOptions.workerSrc = `https://cdnjs.cloudflare.com/ajax/libs/pdf.js/${PDFJS.version}/pdf.worker.js`


回答:

看报错应该是不支持可选链操作符语法导致的语法错误
安装

npm install @babel/plugin-proposal-optional-chaining --save-dev

配置
在babel.config.js中添加:

module.exports = {

....

plugins:[

// 链判断运算符支持 ?.

'@babel/plugin-proposal-optional-chaining'

]

}


回答:

找一下低版本的包,源码只有es6的

以上是 vue2 中使用pdfjs 项目无法启动? 的全部内容, 来源链接: utcz.com/p/934228.html

回到顶部