A lightweight dependency injection system for Vue 3
Cocoon provides a simple, TypeScript-friendly way to register and consume services in your Vue 3 app, with optional global access and lazy initialization. Works with both <script setup> and the Options API.
Full documentation available here
registerDeps()<script setup> and Options APInpm install vue-cocoon
# or
yarn add vue-cocoon
// main.ts
import { createApp } from "vue"
import App from "./App.vue"
import { registerDeps } from "@your-scope/vue-deps"
import { UserService, AuthService } from "./services"
const app = createApp(App)
// You can register instances
// Or factories for lazy instantiation
registerDeps(app, {
userService: () => new UserService(),
authService: () => new AuthService(),
})
app.mount("#app")
// component.vue
<script setup lang="ts">
import { deps } from "@cocoon"
const { userService, authService } = deps()
userService.login()
authService.logout()
</script>