使用“这个"在 Vuejs 2.0 中

编程入门 行业动态 更新时间:2024-10-09 11:28:58
本文介绍了使用“这个"在 Vuejs 2.0 中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时送ChatGPT账号..

我是 VueJS 的新手.我有一个小问题,我无法弄清楚.希望有人能给我一个提示.

I am new in VueJS. I am having a small problem that i could not figure it out. Hope some one can give me a hint.

我正在创建一个语音搜索按钮,基本上当我单击语音按钮时,它会记录我的声音并将其打印到表单中的输入属性.

I am creating a voice search button, basically when i click the voice button then it would record my voice and print it out to the input attribute in form.

<input type="text" name="inputSearch" id="inputSearch"
v-model="inputSearch" class="form-control" x-webkit-speech>

这是我在 VueJS 中的脚本

This is my script in VueJS

<script>
export default {
        data() {
          return {
                    inputSearch: '',
                    show: false
                 }
        },
        methods: {
          voiceSearch: function(event){
                    this.inputSearch = '';
                    this.show = false;
                    if (window.hasOwnProperty('webkitSpeechRecognition')) {
                    var recognition             = new webkitSpeechRecognition();
                    recognition.continuous      = false;
                    recognition.interimResults  = false;
                    recognition.lang            = "en-US";
                    recognition.start();
                    recognition.onresult = function(e) {
                    this.inputSearch = e.results[0][0].transcript;
                     recognition.stop();
                        };
                    recognition.onerror = function(e) {
                          alert('There are something wrong...');
                          recognition.stop();
                    };



                    }else {
                      alert('Your browser does not support HTML5/WebKitSpeech. You are not able to use this functionality');
                    }

          }

        }
    }
</script>

我可以从语音识别中获取文本,但无法在输入表单中显示.

I am able to get the text from the voice recoginization but can not show it in the input form.

谢谢,

推荐答案

使用 ES6 的箭头语法代替 function ,这样可以保持 this 的作用域不变,如下所示:

Instead of using function use arrow syntax of ES6, which keeps scope of this intact, like this:

                recognition.onresult = (e) => {
                   this.inputSearch = e.results[0][0].transcript;
                   recognition.stop();
                };
                recognition.onerror = function(e) {
                      alert('There are something wrong...');
                      recognition.stop();
                };

或其他选项是将 this 保存在其他变量中并使用该变量,如下所示:

or other option is to save this in some other variable and use that variable like following:

                var that = this
                recognition.onresult = function(e) {
                  that.inputSearch = e.results[0][0].transcript;
                  recognition.stop();
                };
                recognition.onerror = function(e) {
                      alert('There are something wrong...');
                      recognition.stop();
                };

您可以在此处查看我的类似答案.

You can have a look at my similar answer here.

这篇关于使用“这个"在 Vuejs 2.0 中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

更多推荐

[db:关键词]

本文发布于:2023-04-27 00:44:09,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1145657.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:quot   Vuejs

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!