ReferenceError:窗口未在打字稿中定义

时间:2021-07-27 13:33:30

标签: typescript barcode-scanner

我正在制作一个条形码扫描仪来扫描 code128 和 pdf417,我正在使用 react-qr-barcode-scanner 在打字稿中这样做。但是在运行代码时出现此错误:

Server Error
ReferenceError: window is not defined

This error happened while generating the page. Any console logs will be displayed in the terminal window.

这是我的代码:(ScannerTest.tsx)

import { useState } from 'react';
import BarcodeScannerComponent from 'react-qr-barcode-scanner';


export const ScannerTest = () => {
    const [camera, setCamera] = useState(false);
    const [data, setData] = useState("Not Found");
    return (
        <div >
            <div>SCANNER!</div>
            <button onClick={() => setCamera(!camera)}>
                {camera ? "Stop" : "Start"}
            </button>
            <div className="container">
                {camera &&
                    <div>
                        <BarcodeScannerComponent
                            width={500}
                            height={500}
                            onUpdate={(err, result) => {
                                if (result) {
                                    setData(result?.text)
                                }
                            }}
                        />
                        <p>{data}</p>
                    </div>}
            </div>
        </div>
    );
}

export default ScannerTest;

我怎样才能避免这个错误?

0 个答案:

没有答案