One method I like is using reactive, and toRefs
import { reactive, toRefs, computed } from "@vue/composition-api"; import { Article } from "../models"; import api from "../api"; const state = reactive({ articles: [] as Article[], totalLikes: computed(() => state.articles.reduce((p, c) => p + c.likes, 0)), }) const loadArticles = () => api.load().then(articles => { state.articles = articles; }); const like = (incoming: Article) => api.like(incoming).then(res => { const index = state.articles.findIndex(a => a.id === res.id); state.articles.splice(index, 1, res); }); export default { ...toRefs(state), loadArticles, like };
Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment's permalink.
Hide child comments as well
Confirm
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
One method I like is using reactive, and toRefs