Vue
HTML
<script src="https://cdn.jsdelivr.net/npm/@vue/composition-api@1.0.0-rc.8"></script>
<div id="app">
<h2 @click="ctx.fn">{{ ctx.counter }} {{counter}} </h2>
</div>
Vue
const { reactive, toRefs } = VueCompositionAPI;
const useCounter = () => {
const ctx = reactive({
counter: 2,
// ...
});
const fn = () => {
console.log('Called', ctx.counter);
}
setInterval(() => ctx.counter += 1, 1000);
return {...toRefs(ctx), fn};
}
new Vue({
el: "#app",
setup() {
const ctx = useCounter();
const { counter } = useCounter();
return { ctx, counter };
}
})