错误 TS2304:找不到名称 'QRScanner'

error TS2304: Cannot find name 'QRScanner'

我参考 https://www.npmjs.com/package/cordova-plugin-qrscanner 并尝试在我的移动应用程序中创建 QR 扫描功能。

所以对于第一步,我在我的 ts 文件中创建以下代码:

function onDone(err, status){

      if (err) {
       // here we can handle errors and clean up any loose ends.
       console.error(err);
      }
      if (status.authorized) {
        // W00t, you have camera access and the scanner is initialized.
        // QRscanner.show() should feel very fast.
      } else if (status.denied) {
       // The video preview will remain black, and scanning is disabled. We can
       // try to ask the user to change their mind, but we'll have to send them
       // to their device settings with `QRScanner.openSettings()`.
      } else {
        // we didn't get permission, but we didn't get permanently denied. (On
        // Android, a denial isn't permanent unless the user checks the "Don't
        // ask again" box.) We can ask again at the next relevant opportunity.
      }
    }

    QRScanner.prepare(onDone);

但是,我在尝试构建时遇到错误。

TS2304: Cannot find name 'QRScanner'

以下是我的版本:

而我在 package.json 中的 cordova-plugin-qrscanner 版本是:

"@types/cordova-plugin-qrscanner": "^1.0.31",
"cordova-plugin-qrscanner": "^3.0.1",

美好的一天,

我找到了解决办法,以下是我的一些理解。 此代码适用于 angular 的旧版本。 我正在使用 angular 10,因此,需要先定义 QRScanner 对象。 这是:

declare var QRScanner: any;

或使用 ( < any > window).QRScanner 获取 QRScanner 插件的访问权限。