<small id='Krshg'></small><noframes id='Krshg'>

    • <bdo id='Krshg'></bdo><ul id='Krshg'></ul>

  • <i id='Krshg'><tr id='Krshg'><dt id='Krshg'><q id='Krshg'><span id='Krshg'><b id='Krshg'><form id='Krshg'><ins id='Krshg'></ins><ul id='Krshg'></ul><sub id='Krshg'></sub></form><legend id='Krshg'></legend><bdo id='Krshg'><pre id='Krshg'><center id='Krshg'></center></pre></bdo></b><th id='Krshg'></th></span></q></dt></tr></i><div id='Krshg'><tfoot id='Krshg'></tfoot><dl id='Krshg'><fieldset id='Krshg'></fieldset></dl></div>

    1. <tfoot id='Krshg'></tfoot>
      1. <legend id='Krshg'><style id='Krshg'><dir id='Krshg'><q id='Krshg'></q></dir></style></legend>

        Vuejs使用addEventListener的事件如何触发执行函数的this

        当我们在Vuejs中使用addEventListener添加事件监听器时,我们需要注意事件处理函数的this指向问题。如果我们使用传统的写法编写事件监听函数,那么this指向的就是监听器所在的DOM元素。在Vuejs中,我们的事件处理函数需要绑定到Vue实例上,这样才能使用Vue实

                <bdo id='lInbV'></bdo><ul id='lInbV'></ul>
                <legend id='lInbV'><style id='lInbV'><dir id='lInbV'><q id='lInbV'></q></dir></style></legend>
              • <i id='lInbV'><tr id='lInbV'><dt id='lInbV'><q id='lInbV'><span id='lInbV'><b id='lInbV'><form id='lInbV'><ins id='lInbV'></ins><ul id='lInbV'></ul><sub id='lInbV'></sub></form><legend id='lInbV'></legend><bdo id='lInbV'><pre id='lInbV'><center id='lInbV'></center></pre></bdo></b><th id='lInbV'></th></span></q></dt></tr></i><div id='lInbV'><tfoot id='lInbV'></tfoot><dl id='lInbV'><fieldset id='lInbV'></fieldset></dl></div>
                <tfoot id='lInbV'></tfoot>

                    <tbody id='lInbV'></tbody>

                  <small id='lInbV'></small><noframes id='lInbV'>

                • 当我们在Vuejs中使用addEventListener添加事件监听器时,我们需要注意事件处理函数的this指向问题。如果我们使用传统的写法编写事件监听函数,那么this指向的就是监听器所在的DOM元素。在Vuejs中,我们的事件处理函数需要绑定到Vue实例上,这样才能使用Vue实例中的数据和方法。

                  下面是一些如何解决Vuejs中addEventListener事件函数this指向问题的方法:

                  方法一:使用箭头函数

                  在Vuejs中,我们可以使用箭头函数来解决addEventListener事件函数this指向问题。箭头函数的this指向是在定义函数时确定的,而非在执行函数时确定的。

                  <template>
                    <div>
                      <button id="btn">Click Me</button>
                    </div>
                  </template>
                  <script>
                    export default {
                      mounted() {
                        const btn = document.getElementById('btn');
                        btn.addEventListener('click', () => {
                          // 在箭头函数中,this指向Vue实例
                          this.clickHandler();
                        });
                      },
                      methods: {
                        clickHandler() {
                          alert('Clicked!');
                        }
                      }
                    }
                  </script>
                  

                  在上面的示例中,我们使用箭头函数来定义了click事件的处理函数。这样,我们就可以在箭头函数中使用Vue实例中的数据和方法。

                  方法二:使用.bind()方法

                  另一个解决addEventListener事件函数this指向问题的方法是使用.bind()方法。我们可以使用.bind()方法将事件处理函数中的this指向Vue实例。例如:

                  <template>
                    <div>
                      <button id="btn2">Click Me Too</button>
                    </div>
                  </template>
                  <script>
                    export default {
                      mounted() {
                        const btn2 = document.getElementById('btn2');
                        btn2.addEventListener('click', this.clickHandler.bind(this));
                      },
                      methods: {
                        clickHandler() {
                          alert('Clicked Again!');
                        }
                      }
                    }
                  </script>
                  

                  在上面的示例中,我们使用.bind()方法将click事件的处理函数中的this指向Vue实例。这样,我们就可以在事件处理函数中使用Vue实例中的数据和方法。

                  无论哪种方法,都可以解决addEventListener事件函数this指向问题,让我们在Vuejs中更加方便地使用事件监听函数。

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  layui实现图片上传成功后回显点击放大图片功能,html代码部分: !-- html代码--div class="layui-form-item" label class="layui-form-label"上传图片/label div class="layui-input-block" button type="button" class="layui-btn" id="license-auth-letter-
                  Layui实现数据表格中鼠标悬停图片放大离开时恢复原图的效果,最终效果如下图所示: 实现代码如下,在done函数中调用hoverOpenImg方法 var tableIns = window.demoTable = table .render({ elem : '#idTest', id : 'idTest', url : '/postData', //width : 150
                  我们在用到layui时候,需要点击文本输入框调起弹出选择框并选择内容,这个要怎么操作呢?以下两种方法可以参考: 1、点击名称,弹出信息弹框,选择表格中的某一行,实现效果如下: html页面代码 !--计量器具弹出层-- div id="equipment" lay-filter="equipmen
                  https的网站如果引用百度地图,会出现加载不了的问题,这是因为涉及到跨域问题,网站是https的,但是引用百度地图的是http的,这个要怎么操作呢? 比如我引用的地址:http://api.map.baidu.com/api?v=2.0ak=AK显示 后来看了一下,少了一个s=1字段,加一下s=1
                  做小程序项目的时候,客户提了一个功能需求优化,就是长按文字需要复制全部内容,因为有的手机支持全选复制,有的手机不支持全选复制。 通过设置系统剪贴板的内容和获取系统剪贴板的内容实现复制功能 html相关代码: van-field value="{{form.contactPhone}}"
                  由于项目功能需要,要实现对table中的行实现拖拽排序功能,找来找去发现Sortable.js能很好的满足这个需求,而且它还是开源的,于是乎就开始学习使用Sortable.js 特点 轻量级但功能强大 移动列表项时有动画 支持触屏设备和大多数浏览器(IE9及以下除外) 支持
                • <i id='xtl9J'><tr id='xtl9J'><dt id='xtl9J'><q id='xtl9J'><span id='xtl9J'><b id='xtl9J'><form id='xtl9J'><ins id='xtl9J'></ins><ul id='xtl9J'></ul><sub id='xtl9J'></sub></form><legend id='xtl9J'></legend><bdo id='xtl9J'><pre id='xtl9J'><center id='xtl9J'></center></pre></bdo></b><th id='xtl9J'></th></span></q></dt></tr></i><div id='xtl9J'><tfoot id='xtl9J'></tfoot><dl id='xtl9J'><fieldset id='xtl9J'></fieldset></dl></div>
                    <legend id='xtl9J'><style id='xtl9J'><dir id='xtl9J'><q id='xtl9J'></q></dir></style></legend>

                      <tbody id='xtl9J'></tbody>
                        <bdo id='xtl9J'></bdo><ul id='xtl9J'></ul>

                          <small id='xtl9J'></small><noframes id='xtl9J'>

                          <tfoot id='xtl9J'></tfoot>