我还将我的文件复制到 /public 目录,并从node_modules
那里移动了除 pspdfkit.js 之外的所有内容,并更新了我的baseUrl
变量以指向该目录。
然后,当我尝试使用npm run start
on 在本地运行我的 react 应用程序时localhost:3000
,我从 PSPDFkit 获得了加载动画,但它挂断了,控制台给了我一个错误:
使用 WASM 方法
开始http://localhost:3000/pspdfkit-lib/pspdfkit.wasm下载。
未捕获(承诺)类型错误:无法在“WebAssembly”上执行“编译”:响应 MIME 类型不正确。预期的“应用程序/wasm”。
我知道上面有一个故障排除主题,可在此处找到,但我仍然无法解决我的问题。
有什么想法吗?
我的组件文件:
import React, {Component} from 'react';
import PSPDFKitWeb from 'pspdfkit';
class PSPDFKit extends Component {
constructor(props, context){
super(props, context);
this._instance = null;
this._container = null;
this.onRef = this.onRef.bind(this);
this.load = this.load.bind(this);
this.unload = this.unload.bind(this);
}
onRef(container) {
this._container = container;
}
//function that loads PSPDFKit library when
async load(props) {
console.log(`Loading ${props.pdfUrl}`);
this._instance = await PSPDFKitWeb.load({
disableWebAssemblyStreaming: true,
pdf: props.pdfUrl,
container: this._container,
licenseKey: props.licenseKey,
baseUrl: props.baseUrl
});
console.log("Successfully mounted PSPDFKit", this._instance);
}
unload() {
PSPDFKitWeb.unload(this._instance || this._container);
this._instance = null;
}
componentDidMount() {
this.load(this.props);
}
componentDidUpdate(nextProps) {
this.unload();
this.load(nextProps);
}
componentWillUnmount() {
this.unload();
}
render() {
return <div ref={this.onRef} style={{ width: "100%", height: "100%", position: "absolute" }} />;
}
}
export default PSPDFKit