All Projects → dashersw → vuelve

dashersw / vuelve

Licence: MIT license
A declarative syntax for the Composition API in Vue 3.

Programming Languages

javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to vuelve

v-bucket
📦 Fast, Simple, and Lightweight State Manager for Vue 3.0 built with composition API, inspired by Vuex.
Stars: ✭ 42 (+10.53%)
Mutual labels:  vue3, composition-api, vue3-composition-api
v-pip
🖼 Tiny vue wrapper for supporting native picture-in-picture mode.
Stars: ✭ 30 (-21.05%)
Mutual labels:  vue3, composition-api
vue-bootstrap-boilerplate
📦 Vue 2/3, Bootstrap 5, Vuex, Vue-Router, Sass/Scss, ESLint, Axios (switch to vue3 branch)
Stars: ✭ 86 (+126.32%)
Mutual labels:  vue3, vue3-composition-api
vue3-demo
💡 vue3新特性示例: 响应式API、组合式API、TodoMVC
Stars: ✭ 114 (+200%)
Mutual labels:  vue3, composition-api
vue-unstated
A tiny state management library for Vue Composition API.
Stars: ✭ 30 (-21.05%)
Mutual labels:  vue3, composition-api
lsp-volar
Language support for Vue3
Stars: ✭ 20 (-47.37%)
Mutual labels:  vue3, vue3-composition-api
vue-snippets
Visual Studio Code Syntax Highlighting For Vue3 And Vue2
Stars: ✭ 25 (-34.21%)
Mutual labels:  vue3, composition-api
2019-ncov-vue3-version
新型冠状病毒实时疫情 Vue-Compostion-Api版本 (Vue3 + TypeScript)
Stars: ✭ 55 (+44.74%)
Mutual labels:  vue3, composition-api
Vue3 News
🔥 Find the latest breaking Vue3、Vue CLI 3+ & Vite News. (2021)
Stars: ✭ 2,416 (+6257.89%)
Mutual labels:  vue3, composition-api
Vue Admin Beautiful
🚀🚀🚀vue3 admin,vue3.0 admin,vue后台管理,vue-admin,vue3.0-admin,admin,vue-admin,vue-element-admin,ant-design,vue-admin-beautiful-pro,vab admin pro,vab admin plus主线版本基于element-plus、element-ui、ant-design-vue三者并行开发维护,同时支持电脑,手机,平板,切换分支查看不同的vue版本,element-plus版本已发布(vue3,vue3.0,vue,vue3.x,vue.js)
Stars: ✭ 10,968 (+28763.16%)
Mutual labels:  vue3, vue3-composition-api
v-intl
Add i18n to your awesome Vue 3 app 🔉
Stars: ✭ 13 (-65.79%)
Mutual labels:  vue3, composition-api
vue-devui-early
Vue3版本的DevUI组件库。本仓库已迁移至:https://github.com/DevCloudFE/vue-devui
Stars: ✭ 39 (+2.63%)
Mutual labels:  vue3, vue3-composition-api
vue-next-rx
RxJS integration for Vue next
Stars: ✭ 31 (-18.42%)
Mutual labels:  vue3, composition-api
vue3-form-validation
Vue Composition Function for Form Validation
Stars: ✭ 18 (-52.63%)
Mutual labels:  vue3, composition-api
vue-reuse
基于composition-api的hooks函数库
Stars: ✭ 28 (-26.32%)
Mutual labels:  vue3, composition-api
vue-admin-better
🚀🚀🚀vue admin,vue3 admin,vue3.0 admin,vue后台管理,vue-admin,vue3.0-admin,admin,vue-admin,vue-element-admin,ant-design,vue-admin-beautiful-pro,vab admin pro,vab admin plus,vue admin plus,vue admin pro
Stars: ✭ 12,962 (+34010.53%)
Mutual labels:  vue3, vue3-composition-api
vue3-docs
vue中文社区,vue3 中文文档
Stars: ✭ 180 (+373.68%)
Mutual labels:  vue3, vue3-composition-api
vue3-realworld-example-app
Explore the charm of Vue composition API! Vite?
Stars: ✭ 364 (+857.89%)
Mutual labels:  vue3, composition-api
vuse-rx
Vue 3 + rxjs = ❤
Stars: ✭ 52 (+36.84%)
Mutual labels:  vue3, composition-api
taro3-vue3-template
一个基于 Taro3 和 Vue3 框架微信小程序模版。 核心技术采用Taro3、Vue3、TypeScript、NutUi、Vux4/Pinia、VueUse
Stars: ✭ 115 (+202.63%)
Mutual labels:  vue3, vue3-composition-api

vuelve

npm version Build Status Coverage Status dependencies Status GitHub license FOSSA Status

Vuelve allows you to create declarative composables in Vue 3.

Motivation

Composition in software is an ideal way to build scalable solutions, and it's often preferred to other methods of code sharing. However, declarative programming is a better ideal. The more we can get away from imperative programming, the easier our programs become to reason about.

Vue 2 was a great step in the right direction with its declarative Options API. Vue 3, in this regard, is a step back, because it requires you to implement all the functionality in an imperative way. You are forced to use a single scope for all your variables and methods, and even worse, to mix the declaration of your lifecycle hooks with your business logic.

We believe this leads to a confusion in bigger codebases, and a better solution can exist, merging the best of both worlds. Enter Vuelve.

Installation

Install Vuelve via npm:

$ npm i vuelve

Usage

Before we start, let's have a look at an example that uses the canonical Composition API. The following example is taken from the Composition API Introduction in Vue.js documentation.

useUserRepositories.js (The composable)

import { fetchUserRepositories } from '@/api/repositories'
import { ref, onMounted, watch } from 'vue'

export default function useUserRepositories(user) {
  const repositories = ref([])
  const getUserRepositories = async () => {
    repositories.value = await fetchUserRepositories(user.value)
  }

  onMounted(getUserRepositories)
  watch(user, getUserRepositories)

  return {
    repositories,
    getUserRepositories,
  }
}

UserRepositories.vue (The component that uses the composable)

import useUserRepositories from './useUserRepositories'
import { toRefs } from 'vue'

export default {
  props: { user: String },
  setup(props) {
    const { user } = toRefs(props)

    const { repositories, getUserRepositories } = useUserRepositories(user)

    return { repositories }
  },
}

Now let's see how we can improve this imperative, single-scope code in the composable.

useUserRepositories.js

import { fetchUserRepositories } from '@/api/repositories'

// use the native export syntax instead of returning variables
export const repositories = []
// exported variables are automatically converted to refs, so no need to explicitly declare refs

export async function getUserRepositories() {
  // access props and exported variables on the this object
  this.repositories.value = await fetchUserRepositories(user.value)
}

export default {
  props: ['user'], // declare your parameters as props
  mounted: getUserRepositories, // easily declare your lifecycle hooks
  watch: {
    user: getUserRepositories,
  },
}

Here we make use of the export keyword instead of arbitrary return statements, and we have real scope in the module where we clearly differentiate between the variables and functions we export, local variables, reactive variables, and lifecycle hooks. Notice how we didn't have to pollute our application logic with imperative lifecycle calls, but were able to separate them. Also notice that our composable is now free of its Vue dependency and is a plain JavaScript file. Theoretically, this allows us to easily migrate this composable to another environment.

Now let's have a look at how we can use this in our component.

UserRepositories.vue

// import Vuelve in your component
import vuelve from 'vuelve'
// import the composable using the "* as" syntax
import { * as useUserRepositories } from './useUserRepositories'
import { toRefs } from 'vue'

export default {
  props: { user: String },
  setup() {
    const { user } = toRefs(props)

    // use Vuelve on the composable
    const { repositories, getUserRepositories } = vuelve(useUserRepositories)(user)

    return { repositories }
  }
}

Here you can see that we had to slightly change how we import our composable and how we convert it back to the regular Composition API with Vuelve.

One might want to hide the complexity of importing and using Vuelve in one's components, and might want to prefer to tuck this complexity inside the composable. This approach also has the added benefit of being a drop-in replacement for the Composition API.

In order to allow this, Vuelve supports two additional syntaxes. In the future we wish to settle on one syntax, but before that we'd like to see which syntax the community adopts.

Syntax 2

useUserRepositories.js

import vuelve from 'vuelve'

import { fetchUserRepositories } from '@/api/repositories'

// declare the variable locally
const repositories = []

async function getUserRepositories() {
  // access props and returned variables on the this object
  this.repositories.value = await fetchUserRepositories(user.value)
}

export default vuelve({
  props: ['user'], // declare your parameters as props
  mounted: getUserRepositories, // easily declare your lifecycle hooks
  watch: {
    user: getUserRepositories,
  },
  returns: { repositories, getUserRepositories }, // define your returns declaratively
})

UserRepositories.vue

import useUserRepositories from './useUserRepositories'
import { toRefs } from 'vue'

export default {
  props: { user: String },
  setup(props) {
    const { user } = toRefs(props)

    const { repositories, getUserRepositories } = useUserRepositories(user)

    return { repositories }
  },
}

As you can see, UserRepositories.vue is exactly the same as the original version that uses the composable made with Composition API. In this case, Vuelve composables are a drop-in replacement.

If you don't prefer returns keyword in your composables, Vuelve supports a third syntax:

Syntax 3

useUserRepositories.js

import vuelve from 'vuelve'

import { fetchUserRepositories } from '@/api/repositories'

// declare the variable locally
const repositories = []

async function getUserRepositories() {
  // access props and returned variables on the this object
  this.repositories.value = await fetchUserRepositories(user.value)
}

export default vuelve(
  {
    props: ['user'], // declare your parameters as props
    mounted: getUserRepositories, // easily declare your lifecycle hooks
    watch: {
      user: getUserRepositories,
    },
  },
  { repositories, getUserRepositories } // define your returns declaratively)
)

Notice here that the returns object is passed as the second parameter to Vuelve.

Future work

Vuelve is not complete in its support for all lifecycle hooks the Composition API provides. We are still working on adding more functionality and the goal is to support 100% of the Composition API syntax.

Since Vuelve is declarative in its nature, there will be certain tricks that you won't be able to do with Vuelve which would be possible with the raw Composition API, but this would involve depending too much on imperative order of execution, and we believe this is a bad practice.

Contribution

We are looking for contributors actively to bring missing features of the Composition API into Vuelve. Furthermore, as the community adopts Composition API, we need to expand our examples of declarative composables.

If you would like to see a feature implemented or want to contribute a new feature, you are welcome to open an issue to discuss it and we will be more than happy to help.

If you choose to make a contribution, please fork this repository, work on a feature and submit a pull request. We offer timely and thoughtful code reviews.

License

MIT License

Copyright (c) 2020 Armagan Amcalar

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].