Javascript 类型脚本中的NDEFReader
我正在尝试使用Javascript 类型脚本中的NDEFReader,javascript,reactjs,nfc,webnfc,Javascript,Reactjs,Nfc,Webnfc,我正在尝试使用NDEFReader()进行NFC扫描/写入。 这项功能适用于Chrome81(你可以在ChromeBeta版的手机上通过下面的链接试用) ,, 要启用此功能,您需要进入chrome://flags/并启用实验性Web平台功能 问题是,我不能让这项工作在反应。我使用带有TypeScript和控制台输出的create react应用程序: 找不到名称“NDEFReader” 我认为这会导致网页检查。我已经尝试更改tsconfig.json中的一些设置,但没有任何效果。有人知道如何启
NDEFReader()
进行NFC扫描/写入。
这项功能适用于Chrome81(你可以在ChromeBeta版的手机上通过下面的链接试用)
,,
要启用此功能,您需要进入chrome://flags/
并启用实验性Web平台功能
问题是,我不能让这项工作在反应。我使用带有TypeScript和控制台输出的create react应用程序:
找不到名称“NDEFReader”
我认为这会导致网页检查。我已经尝试更改tsconfig.json中的一些设置,但没有任何效果。有人知道如何启用实验性js/ts编译来启用此功能吗?这不是关于网页检查、tsconfig或“实验性JavaScript” 只是没有可用的
NDEFReader()
类型,所以TypeScript认为您有输入错误
您可以在源代码树中使用类似于extra globals.d.ts
(名称无关紧要,只要它是.d.ts
)的文件为NDEFReader
插入一个类型。这基本上告诉TypeScript,全局窗口界面有一个额外的字段,NDEFReader
,您不知道它的类型:
声明全局{
界面窗口{
NDEFReader:任何;
}
}
出口{};
网络NFC人员在
//Web NFC的类型定义
//项目:https://github.com/w3c/web-nfc
//定义:Takefumi Yoshii
//打字稿版本:3.9
//此类型定义引用了WebIDL。
// https://w3c.github.io/web-nfc/#actual-idl指数
界面窗口{
NDEFMessage:NDEFMessage
}
声明类NDEFMessage{
构造函数(messageInit:NDEFMessageInit)
记录:ReadonlyArray
}
声明接口NDEFMessageInit{
记录:NDEFRecordInit[]
}
声明类型NDEFRecordDataSource=string | BufferSource | NDEFMessageInit
界面窗口{
NDEFRecord:NDEFRecord
}
声明类NDEFRecord{
构造函数(recordInit:NDEFRecordInit)
只读记录类型:字符串
只读媒体类型?:字符串
只读id?:字符串
只读数据?:数据视图
只读编码?:字符串
只读语言?:字符串
存储记录?:()=>NDEFRecord[]
}
声明接口NDEFRecordInit{
记录类型:字符串
mediaType?:字符串
id?:字符串
编码?:字符串
朗:字符串
数据?:ndefreRecordDataSource
}
声明类型NDEFMessageSource=string | BufferSource | ndefMessageUnit
界面窗口{
NDEFReader:NDEFReader
}
声明类NDEFReader扩展事件目标{
构造函数()
onreading:(this:this,event:ndefradingevent)=>任意
onreadingerror:(this:this,error:Event)=>any
扫描:(选项?:NDEFScanOptions)=>承诺
写:(
消息:NDEFMessageSource,
选项?:NDEFWriteOptions
)=>承诺
}
界面窗口{
NDEFReadingEvent:NDEFReadingEvent
}
声明类NDEFReadingEvent扩展事件{
构造函数(类型:string,readingEventInitDict:ndefradingEventInitnit)
序列号:字符串
消息:NDEFMessage
}
接口NDEFReadingEventInit扩展了EventInit{
序列号?:字符串
消息:NDEFMessageInit
}
接口NDEFWRITE选项{
覆盖?:布尔值
信号?:中止信号
}
接口NDEFScanOptions{
信号:中止信号
}
在React--typescript:Demo中找到了工作解决方案:
// Type definitions for Web NFC
// Project: https://github.com/w3c/web-nfc
// Definitions by: Takefumi Yoshii <https://github.com/takefumi-yoshii>
// TypeScript Version: 3.9
// This type definitions referenced to WebIDL.
// https://w3c.github.io/web-nfc/#actual-idl-index
interface Window {
NDEFMessage: NDEFMessage
}
declare class NDEFMessage {
constructor(messageInit: NDEFMessageInit)
records: ReadonlyArray<NDEFRecord>
}
declare interface NDEFMessageInit {
records: NDEFRecordInit[]
}
declare type NDEFRecordDataSource = string | BufferSource | NDEFMessageInit
interface Window {
NDEFRecord: NDEFRecord
}
declare class NDEFRecord {
constructor(recordInit: NDEFRecordInit)
readonly recordType: string
readonly mediaType?: string
readonly id?: string
readonly data?: DataView
readonly encoding?: string
readonly lang?: string
toRecords?: () => NDEFRecord[]
}
declare interface NDEFRecordInit {
recordType: string
mediaType?: string
id?: string
encoding?: string
lang?: string
data?: NDEFRecordDataSource
}
declare type NDEFMessageSource = string | BufferSource | NDEFMessageInit
interface Window {
NDEFReader: NDEFReader
}
declare class NDEFReader extends EventTarget {
constructor()
onreading: (this: this, event: NDEFReadingEvent) => any
onreadingerror: (this: this, error: Event) => any
scan: (options?: NDEFScanOptions) => Promise<void>
write: (
message: NDEFMessageSource,
options?: NDEFWriteOptions
) => Promise<void>
}
interface Window {
NDEFReadingEvent: NDEFReadingEvent
}
declare class NDEFReadingEvent extends Event {
constructor(type: string, readingEventInitDict: NDEFReadingEventInit)
serialNumber: string
message: NDEFMessage
}
interface NDEFReadingEventInit extends EventInit {
serialNumber?: string
message: NDEFMessageInit
}
interface NDEFWriteOptions {
overwrite?: boolean
signal?: AbortSignal
}
interface NDEFScanOptions {
signal: AbortSignal
}