I have a simple vue codesandbox demo.
In this demo, I pass in a method to a child component (defined in child) via slots from parent. However that method does not execute when clicked on button to which this method is attached.
I want to know WHY child methods when passed (from parent) via slots does not work. I'm more interested in the logic behind this.
App.vue
<template>
<div id="app">
<img width="25%" src="./assets/logo.png" />
<user>
<button @click="changeName('Don')">Change Name</button>
</user>
</div>
</template>
<script>
import user from "./components/user";
export default {
name: "App",
components: {
user
},
data: function() {
return {
msg: "Name is Bond.. James Bond"
};
}
};
</script>
User.vue
<template>
<div class="wrapper">
<h2>My name is "{{myName}}"</h2>
<slot></slot>
</div>
</template>
<script>
export default {
data() {
return {
myName: 'Bond'
}
},
methods: {
changeName: function(newName){
this.myName = newName
}
}
};
</script>
Thank You.