【问题标题】:Web Speech API SpeechRecognition not defined when using React.js使用 React.js 时未定义 Web Speech API SpeechRecognition
【发布时间】:2020-10-03 05:25:51
【问题描述】:

我将 React.js 与 Web Speech API 的 SpeechRecognition 一起使用,但是它不起作用并且我收到错误“ReferenceError:SpeechRecognition 未定义”。我使用的代码直接来自 SpeechRecognition 文档:

const SpeechRecognition = SpeechRecognition || webkitSpeechRecognition;
const recognition = new SpeechRecognition();

第一行导致错误,但没有它,第二行将导致相同的错误。我该如何解决这个问题?

【问题讨论】:

    标签: javascript reactjs speech-recognition webspeech-api


    【解决方案1】:

    试试window.SpeechRecognition || window.webkitSpeechRecognition;

    请参阅Using the Web Speech API 以进一步解释为什么需要window. 前缀。

    【讨论】:

    • 你的答案是正确的,但如果你添加解释会更有帮助。
    • 它还没有被 Firefox 标准化和支持。因此前缀
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-08-21
    • 2015-12-07
    • 2013-09-25
    • 2021-07-18
    相关资源
    最近更新 更多