aff*_*eto 3 web-bluetooth angular
我将示例代码集成到我的 Angular 6 项目中。但是有一些编译错误。这些错误之一是:“导航器”类型上不存在“蓝牙”属性。
为什么会发生此错误,我该如何解决?
nun*_*uda 10
我不确定为什么会发生此错误,但您可以通过安装类型npm install --save-dev @types/web-bluetooth并使用三斜杠指令 来修复它,/// <reference types="web-bluetooth" />如下所示:
/// <reference types="web-bluetooth" />
import { Component } from '@angular/core';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css'],
})
export class AppComponent {
title = 'myApp';
async test() {
try {
const device = await navigator.bluetooth.requestDevice({
filters: [
{
namePrefix: 'test',
},
],
optionalServices: ['test'],
});
} catch (error) {
console.error(error);
}
}
}
Run Code Online (Sandbox Code Playgroud)
使用以下模块来安装各种类型的 web-bluetooth api。您可以使用它来定义导航器 blutooth api 对象的类型。
https://www.npmjs.com/package/@types/web-bluetooth
现在,如果您不需要指定导航器对象(及其属性)的确切类型,则可以执行以下操作:
let mobileNavigatorObject: any = window.navigator;
if(mobileNavigatorObject && mobileNavigatorObject.bluetooth) {
// Here write your logic of mobileNavigatorObject.bluetooth.requestDevice();
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1978 次 |
| 最近记录: |