On gists
Provider->Consumer
•
Patterns.dev
Plugin patterns
Vue.js
Provider.vue
Raw
#
<!-- https://learn-vuejs.github.io/vue-patterns/patterns/#dependency-injection -->
<template>
<div>
<slot v-bind="{ state, actions }" />
</div>
</template>
<script>
export default {
computed: {
state() {
return {
label: 'button',
};
},
actions() {
return {
click: this.click,
};
},
},
methods: {
click() {
console.log('Clicked');
},
},
}
</script>
Consumer.vue
Raw
#
<template>
<div>
<p>{{ props.state.label }}</p>
<button @click="props.actions.click">CLICK</button>
</div>
</template>
app.vue
Raw
#
<template>
<provider v-slot="{ state, actions }">
<consumer v-bind="{ state, actions }" />
</provider>
</template>
<script>
import Provider from './Provider.vue';
import Consumer from './Consumer.vue';
export default {
components: {
Provider,
Consumer,
},
};
</script>