How can I prevent Vuex from interfering with my class instance?

谁都会走 提交于 2019-12-23 20:40:53

问题


I am trying to store an instance of a class in Vuex (the EditorState from ProseMirror). This class is more or less immutable from the outside, meaning that whenever I want to make changes to it, I will just put a new instance of the class into Vuex.

As such, I do not need Vue's change tracking here, and in fact it actually seems to interfere with the internal workings of ProseMirror, so I was wondering if there was a way that I could isolate my object from Vue so that it is treated atomically.


回答1:


I fixed it by taking the suggestion from the top answer on this question and freezing my class instance before giving it to Vuex.

const store = new Store<AppState>({
    state: {
        editor: Object.freeze(editorState), // freeze because Vue reactivity messes it up
        filename: null,
        metadata: {}
    },
    mutations: {
        updateDocument(context, transaction: Transaction) {
            console.log("updateDocument called");

            // freeze again
            context.editor = Object.freeze(context.editor.apply(transaction));
        }
    },
    strict: process.env.NODE_ENV === "development"
});

Since Object.freeze is not recursive, this doesn't have any effect on the internal workings of ProseMirror, but it discourages Vue from trying to modify the object.



来源:https://stackoverflow.com/questions/56412419/how-can-i-prevent-vuex-from-interfering-with-my-class-instance

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!