gpt4 book ai didi

javascript - Angular 2 : Web Speech API - Voice recognition

转载 作者:可可西里 更新时间:2023-11-01 01:49:57 28 4
gpt4 key购买 nike

阅读webkitSpeechRecognition的文档后(Javascript 中的语音识别)我试图在 Angular 2 中实现它.

但是当我这样做的时候:

const recognition = new webkitSpeechRecognition();

typescript 说这个错误:

[ts] Cannot find name 'webkitSpeechRecognition'. any

如果我尝试从 window 中提取 webkitSpeechRecognition:

if ('webkitSpeechRecognition' in window) {

console.log("Enters inside the condition"); // => It's printing

const { webkitSpeechRecognition } = window; // => TypeScript Error
const recognition = new webkitSpeechRecognition();
}

如果我注释最后两行 console.log 被打印,进入条件! webkitSpeechRecognition 存在于窗口内!!但是,如果不注释最后两行,TypeScript 错误现在是这样的:

[ts] Type 'Window' has no property 'webkitSpeechRecognition' and no string index signature.
const webkitSpeechRecognition: any

如何在 Angular 2 中创建新识别?有人试过吗?

最佳答案

最后我解决了创建一个接口(interface)!!

export interface IWindow extends Window {
webkitSpeechRecognition: any;
}

和:

const {webkitSpeechRecognition} : IWindow = <IWindow>window;
const recognition = new webkitSpeechRecognition();

关于javascript - Angular 2 : Web Speech API - Voice recognition,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38087013/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com