简体   繁体   English

在 VueJS 中,FabricJS 实例存储在哪里? Window? 维克斯? 组件数据?

[英]In VueJS, where to store FabricJS instances? Window? Vuex? Component data?

Where I'm suposed to store FabricJS instances in VueJS?我应该在 VueJS 中存储 FabricJS 实例的位置? I tried to store it in Vuex state but Fabric instances has lots of internal methods that update the instance and triggers a "Don't mutate state outside Vuex mutations" error.我试图将它存储在 Vuex state 中,但 Fabric 实例有很多内部方法可以更新实例并触发“不要在 Vuex 突变之外改变 state”错误。

I see you're using Vue2, and to help Vue3 users I'll touch on it as well.我看到你正在使用 Vue2,为了帮助 Vue3 用户,我也会谈到它。

I would want to store a Fabric.js canvas in a way that won't have reactivity injected into it.我想以不会注入反应性的方式存储 Fabric.js canvas。 Otherwise performance will likely be awful.否则性能可能会很糟糕。

To store it in Vuex or component data, you can use Object.freeze() and in Vue3 there is shallowRef .要将其存储在 Vuex 或组件数据中,您可以使用Object.freeze()并且在 Vue3 中有shallowRef Both are described a bit here .两者都在这里有所描述

Another option is to look into some of the state functions in VueUse, which supports Nuxt2/Vue2 and Nuxt3/Vue3.另一种选择是查看支持 Nuxt2/Vue2 和 Nuxt3/Vue3 的 VueUse 中的一些state 函数

Although not as friendly outside of.vue files, provide and inject in Vue2 or Vue3 components can be useful to share objects.尽管在 .vue 文件之外不那么友好,但provideinject Vue2Vue3组件对于共享对象很有用。

Storing in window is easy and convenient, but globals tend to come back to haunt me later.存储在window中既简单又方便,但全局变量稍后会回来困扰我。 I'd use something from the options above to keep the canvas scoped.我会使用上述选项中的一些东西来保持 canvas 的范围。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM