简体   繁体   English

是否可以使用Vue.js触发事件?

[英]Is it possible to trigger events using Vue.js?

I have just started using Vue.js and find myself continuously turning to jQuery to help in certain situations. 我刚刚开始使用Vue.js,并发现自己不断转向jQuery来帮助某些情况。 Most recently I have attempted to, unsuccessfully "trigger" (or emulate) an event, such as an on click or blur event. 最近,我试图“触发”(或模仿)某个事件,例如点击或模糊事件,但未成功。

I know in jQuery you can do the following: 我知道在jQuery中你可以做到以下几点:

$('#my-selector').trigger('click');

But how can this be acheived using Vue.js? 但是如何使用Vue.js实现这一目标呢? I am wanting to move away from using jQuery as much as possible. 我想尽可能地避免使用jQuery。

Take the below as an example: 以下面的例子为例:

<div id="my-scope">
    <button type="button" v-on="click: myClickEvent">Click Me!</button>
</div>

<script>
new Vue({
    el: "#my-scope",
    data: {
        foo: "Hello World"
    },
    methods: {
        myClickEvent: function(e) {
            console.log(e.targetVM);
            alert(this.foo);
        },
        anotherRandomFunciton: function() {
            this.myClickEvent();
        }
    }
});
</script>

If I was to call anotherRandomFunciton , I would like it to emulate (or trigger) the above click event. 如果我要调用anotherRandomFunciton ,我希望它模拟(或触发)上面的click事件。 However, I attempt this the event (or e in the above example) never gets passed to the function. 但是,我尝试这个事件(或上例中的e )永远不会传递给函数。

Does Vue.js have a method for achieving this? Vue.js有实现这个目标的方法吗?

You need to get a reference to your button by using v-el like so: 您需要使用v-el来获取对按钮的引用,如下所示:

<button type="button" @click="myClickEvent" v-el:my-btn>Click Me!</button>

Then, in your method: 然后,在您的方法中:

function anotherRandomFunction() {
    var elem = this.$els.myBtn
    elem.click()
}

It's just a native DOM click event. 它只是一个本机DOM点击事件。 See v-el Documentation 请参阅v-el文档

Or since Vue 2.x: 或者从Vue 2.x开始:

<template>
    <button type="button" @click="myClickEvent" ref="myBtn">
        Click Me!
    </button>
</template>

<script>
export default {
    methods: {
        myClickEvent($event) {
            const elem = this.$refs.myBtn
            elem.click()
        }
    }
}
</script>

Since Vue uses and listens for native DOM events only. 由于Vue仅使用和侦听本机DOM事件。 You can trigger native DOM events using the Element#dispatchEvent like so: 您可以使用Element#dispatchEvent触发本机DOM事件,如下所示:

var elem = this.$els.myBtn; // Element to fire on

var prevented = elem.dispatchEvent(new Event("change")); // Fire event

if (prevented) {} // A handler used event.preventDefault();

This is not exactly ideal or best practice. 这不是完全理想或最佳实践。 Ideally, you should have a function that handles the internals and an event handler that calls that function. 理想情况下,您应该有一个处理内部的函数和一个调用该函数的事件处理程序。 That way, you can call the internals whenever you need to. 这样,您可以随时调用内部。

If someone stumbles upon this, this is how I did it: 如果有人偶然发现这个问题,我就是这样做的:

When using this.$refs.myBtn.click() 使用时this.$refs.myBtn.click()

I get 我明白了

“Uncaught TypeError: this.$refs.myBtn.click is not a function” “Uncaught TypeError:this。$ refs.myBtn.click不是函数”

Changed it to: this.$refs.myBtn.$el.click() 将其更改为: this.$refs.myBtn.$el.click()

To be clear: “ $el ” needs to be added for it to work. 要明确:需要添加“ $el ”才能使其正常工作。

Vue is by its nature narrowly focused – it is intended that other packages like jQuery (EDIT: for jQuery features other than DOM manipulation) to be used with it. Vue公司是由它的性质狭隘的-其意图是像jQuery其他包(编辑:jQuery的比DOM操作功能),要与它使用。 I think using jQuery's trigger is just fine. 我认为使用jQuery的trigger就好了。

However, if you want to create your own events without jQuery, check out dispatchEvent : 但是,如果您想在没有jQuery的情况下创建自己的事件,请查看dispatchEvent

https://developer.mozilla.org/en-US/docs/Web/API/EventTarget/dispatchEvent https://developer.mozilla.org/en-US/docs/Web/API/EventTarget/dispatchEvent

Or, for the particular case of click, there is a method on DOM elements you can use: 或者,对于click的特定情况,您可以使用DOM元素的方法:

https://developer.mozilla.org/en-US/docs/Web/API/HTMLElement/click https://developer.mozilla.org/en-US/docs/Web/API/HTMLElement/click

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM