Vue 3’s new Composition API can elegantly aggregate disparate business code. What if you want to use Composition API in Vue2 projects?
You can use the official plugin @vue/composition-api.
@ vue/composition – API is introduced
For those of you who are not familiar with Composition apis, check out one of my previous articles: Vue Composition API – A Solution for Assembling disparate business code.
Installation:
npm install @vue/composition-api
Copy the code
Before you can use @vue/composition-api, you must first register the plug-in in vue via vue.use ().
import Vue from 'vue'
import VueCompositionAPI from '@vue/composition-api'
Vue.use(VueCompositionAPI)
Copy the code
Use:
import { ref, reactive } from '@vue/composition-api'
Copy the code
When the project is migrated to Vue 3, simply replace @vue/composition-api with Vue.
import { ref, reactive } from 'vue'
Copy the code
Happy Coding~
Reference documentation
- @ vue/composition – API’s official website.