M
M
Maxim Timofeev2018-06-19 17:41:46
Vue.js
Maxim Timofeev, 2018-06-19 17:41:46

How to catch in Vue click outside the element?

Something turned me on.
There is a component, it has a method toggleDropdown(), it perfectly opens and closes my dropdan. When you click on the elements, everything is clear, v-on:click.stop="toggleDropdown"but what to do when the click should be obeyed by anyone? I try like this:

created() {
            document.addEventListener('click', function () {
                this.toggleDropdown();
            });
        },

But after all, there will be no vue in this and, as a result, an error. I'm sure it's just solved, but since I c vue on you, I'm stupid. Or just stupid

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
Roman Kitaev, 2018-06-19
@webinar

document.addEventListener('click', () => this.toggleDropdown());

Or
document.addEventListener('click', this.toggleDropdown.bind(this));

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question