error Unresolved reference scanCode in uni-app

error Unresolved reference scanCode in uni-app

开发环境 版本号 项目创建方式
Windows win10 HBuilderX

示例代码:

const scanQRCode = () => {  
    uni.scanCode({  
        success: (res: ScanCodeSuccess) => {  
            console.log('res: ',res);  

        },  
        fail: (err: ScanCodeFail) => {  
            console.log('err: ',err);  
            // 需要注意的是小程序扫码不需要申请相机权限  
        }  
    });  
}

操作步骤:

  • 保存文件时进行编译到安卓代码报错

预期结果:

  • 应编译正常

实际结果:

5:46:39.939 ‌error: Unresolved reference: uni_scanCode‌
15:46:39.939 at pages/index/index.uvue:72:2
15:46:39.939 70 | 
15:46:39.939 71 |   const scanQRCode = () => { 
15:46:39.939 72 |    uni.scanCode({
15:46:39.939    |    ^ 
15:46:39.939 73 |         success: (res: ScanCodeSuccess) => { 
15:46:39.939 74 |                console.log('res: ',res);

‌error: Unresolved reference: ScanCodeOptions‌
15:46:39.939 at pages/index/index.uvue:72:2
15:46:39.939 70 | 
15:46:39.939 71 |   const scanQRCode = () => { 
15:46:39.939 72 |    uni.scanCode({
15:46:39.939    |    ^ 
15:46:39.939 73 |         success: (res: ScanCodeSuccess) => { 
15:46:39.939 74 |                console.log('res: ',res);

‌error: Unresolved reference: ScanCodeSuccess‌
15:46:39.940 at pages/index/index.uvue:73:22
15:46:39.940 71 |   const scanQRCode = () => { 
15:46:39.940 72 |    uni.scanCode({
15:46:39.940 73 |         success: (res: ScanCodeSuccess) => { 
15:46:39.940    |                        ^ 
15:46:39.940 74 |                console.log('res: ',res);

‌error: Unresolved reference: ScanCodeFail‌
15:46:39.940 at pages/index/index.uvue:77:19
15:46:39.940 75 | 
15:46:39.940 76 |         }, 
15:46:39.940 77 |         fail: (err: ScanCodeFail) => { 
15:46:39.940    |                     ^ 
15:46:39.940 78 |                console.log('err: ',err);
15:46:39.940 79 |          // 需要注意的是小程序扫码不需要申请相机权限
15:49:30.923 App Hide at App.uvue:11

更多关于error Unresolved reference scanCode in uni-app的实战教程也可以访问 https://www.itying.com/category-93-b0.html

2 回复

安卓上 4.71 支持,看你这 66 应该还不支持

更多关于error Unresolved reference scanCode in uni-app的实战教程也可以访问 https://www.itying.com/category-93-b0.html


这个错误是因为在 uni-app 的 uvue 文件中使用了 TypeScript 类型声明,但缺少相应的类型定义文件。

问题出现在代码中的类型注解:

  • uni.scanCode 被识别为 uni_scanCode
  • ScanCodeSuccess
  • ScanCodeFail
  • ScanCodeOptions

解决方案:

  1. 移除类型注解(推荐):
const scanQRCode = () => {  
    uni.scanCode({  
        success: (res) => {  
            console.log('res: ', res);  
        },  
        fail: (err) => {  
            console.log('err: ', err);  
        }  
    });  
}
  1. 或者添加正确的类型导入: 在文件顶部添加:
/// <reference types="[@dcloudio](/user/dcloudio)/types" />
回到顶部