我正在将我的Vue2应用程序迁移到使用@vue/composition-api (Vue版本仍然是2而不是3)。
在Vue2中,我使用像this.$session这样的插件,但是this不能在Vue3上工作。我找到的解决方案是这样使用:
setup (props, context) {
context.root.$session // works fine
context.root.$anotherPlugin
}然而,在Vue3中,context.root是@deprecated,所以当我迁移到Vue3时,它就不再工作了。
在setup()中还有其他方法来访问Vue实例吗?我想,如果我能访问它,我可以正常使用这些插件在Vue3上。
发布于 2020-12-04 14:00:45
使用provide和inject
提供
const app = createApp(App);
app.provide('someVarName', someVar); // Providing to all components here注射
const { inject } = Vue;
...
setup() {
const someVar = inject('someVarName'); // injecting in a component that wants it
}https://stackoverflow.com/questions/65144852
复制相似问题