I have a component my-parent
. In this component, I use a child component my-child
and import an external class MyClass
with an own function exportedFunction
. I tried to use this solution: VueJS accessing externaly imported method in vue component
Basiclly, I use mounted
and the name of the function from the imported class. In methods
I defined a new method, which calls the mounted one from the imported class. Than I pass the created method as property to my child, where I try to call the function with a @click
and pass the parameter there.
Here is my code so far:
my-parent
template:
<template>
<my-child :exportedFunction="callFunction"></my-child>
</template>
<script>
import MyClass from './MyClass';
export default {
mounted() {
exportedFunction()
},
methods: {
callFunction() {
exportedFunction()
}
}
}
</script>
my-child
template:
<template>
<button @click="exportedFunction('hello world!')">Click me!</button>
</template>
<script>
export default {
props: ['exportedFunction']
}
</script>
MyClass
code:
export default class MyClass {
exportedClass(parameter) {
console.log(parameter) // expected 'hello world' from child
}
}
Hope for some help!
I would ditch your
MyClass
component and instead have:my-parent
my-child
As you want to use
MyClass
in your example you can keep it as is and havemy-parent
as: