npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@tanbo/vue-di-plugin

v1.0.5

Published

A dependency injection Library for vue3

Downloads

3

Readme

基于反射依赖注入的 vue 插件

本插件用于解决 vue 框架自带的依赖注入能力较为简单的问题,通过本插件,可以实现自动依赖分析,分层注入,自动关联上下文等功能

安装

npm install @tanbo/vue-di-plugin

配置 tsconfig

在 tsconfig.json 中加入如下配置

{
  "compilerOptions": {
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true
  }
}

使用示例

根依赖

// # deps.ts
import { Injectable } from '@tanbo/vue-di-plugin';

@Injectable()
export class Parent {
  name = 'parent'
}

@Injectable()
export class Child {
  constructor(private parent: Parent) {
  }
}

在创建 App 时提供根依赖

import { createApp } from 'vue'
import App from './App.vue'
import { reflectiveInjectorPlugin, NullInjector, ReflectiveInjector } from '@tanbo/vue-di-plugin';

import { Parent, Child } from './deps'

const rootInjector = new ReflectiveInjector(new NullInjector(), [Parent, Child])
createApp(App)
  // 提供根依赖,如果没有,可以省略
  .use(reflectiveInjectorPlugin, rootInjector)
  .mount('#app')

在组件中使用

import { defineComponent } from 'vue'

import { useReflectiveInjector } from '@tanbo/vue-di-plugin'
import { Child } from './deps'

export default defineComponent({
  name: 'App',
  setup() {
    const injector = useReflectiveInjector()
    console.log(injector)
    console.log(injector.get(Child))
  }
});

小知识

在组件内调用 useReflectiveInjector 时,也可以传入新的 providers,然后再通过返回的 injector 实例获取 provider 提供的类的实例。同时,所有的后代组件也都可以通过 injector 获取到上层组件提供的类的实例。

更多依赖注入的用法请参考 @tanbo/di