Answer the question
In order to leave comments, you need to log in
vue3. How to access a function from a directive?
Code example:
<!DOCTYPE html>
<html>
<head></head>
<body>
<div id="app">
<button v-test>click me</button>
</div>
<script src="https://unpkg.com/[email protected]/dist/vue.global.js"></script>
<script>
(function () {
'use strict';
const App = {};
const TestDirective = {
data() {
return {
someData: [123, 456]
}
},
methods: {
helloMethod() {
console.log('hello from method!');
}
},
mounted() {
// ↓↓↓ Вот тут появляется ошибка "Uncaught TypeError: Cannot read property 'helloMethod' of undefined"
this.helloMethod()
// ↑↑↑
console.log(this.someData);
}
};
const app = Vue.createApp(App);
app.directive('test', TestDirective);
app.mount('#app');
})();
</script>
</body>
</html>
Uncaught TypeError: Cannot read property 'helloMethod' of undefined
Answer the question
In order to leave comments, you need to log in
Read carefully here https://v3.vuejs.org/guide/custom-directive.html , pay special attention to working with arguments.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question