Typescript 使用decorator在vue 3中未定义注入值
我试图用tsyringe在Vue 3中进行依赖项注入,但是当我注入我的组件时,我得到了一个“未定义”的值,下面是代码:Typescript 使用decorator在vue 3中未定义注入值,typescript,vue.js,vue-component,vuejs3,Typescript,Vue.js,Vue Component,Vuejs3,我试图用tsyringe在Vue 3中进行依赖项注入,但是当我注入我的组件时,我得到了一个“未定义”的值,下面是代码: // TYPES.ts export const TYPES = { WARRIOR: Symbol.for("Warrior"), WEAPON: Symbol.for("Weapon"), CONTAINER: Symbol.for("GlobalContainer"), } // gl
// TYPES.ts
export const TYPES = {
WARRIOR: Symbol.for("Warrior"),
WEAPON: Symbol.for("Weapon"),
CONTAINER: Symbol.for("GlobalContainer"),
}
// globalcontainer.ts
import "reflect-metadata";
import {container as globalContainer, DependencyContainer} from "tsyringe";
import {TYPES} from "@/models/Types";
import {Warrior} from "@/interfaces/Warrior";
import {Weapon} from "@/interfaces/Weapon";
import {Ninja} from "@/models/Warrior";
import {Katana} from "@/models/Weapon";
globalContainer.register<Warrior>(TYPES.WARRIOR, Ninja);
globalContainer.register<Weapon>(TYPES.WARRIOR, Katana);
globalContainer.register<DependencyContainer>(TYPES.CONTAINER, {useValue: globalContainer})
export default globalContainer;
// app.Vue
provide: {
[TYPES.CONTAINER]: globalContainer
}
但当我在lifecylecreated
中调用它时,我没有定义,
这就是我所说的
// Home.vue
import { Options, Vue } from "vue-class-component";
import { DependencyContainer } from "tsyringe";
import { Inject } from "@/configs/InjectDecarator";
export default class Home extends Vue {
@Inject(TYPES.CONTAINER)
private _container!: DependencyContainer;
created() {
console.log(this._container!); // undefined
}
}
是否有任何选项可调用此组件上的提供??我的提供者是
[TYPES.CONTAINER]: globalContainer
我在这里使用ts看看这是否有帮助。。。是的,我看到了,vue属性decator在这个问题上使用vue类组件的v7,我使用的是vue类组件v8,所以我要求的是v8:),好吧,也许我会转到v7以更加稳定
[TYPES.CONTAINER]: globalContainer