I am attempting to use Pinia to reserve some global data, and I define a pinia store like this:
import { defineStore } from 'pinia';
export class TreeNode {
constructor(data, parent = null) {
this.data = data;
this.parent = parent;
this.children = [];
}
}
export const useXXXStore = defineStore('xxx', {
state: () => ({
root: new TreeNode('data', null),
current_node: this.root,
}),
})
However, I got this error message after running this program:
XXXStore.js?b287:16 Uncaught (in promise) TypeError: Cannot read properties of undefined (reading 'root')
at state (BoardStateStore.js?b287:16:1)
at setup (pinia.mjs?b318:1196:1)
at eval (pinia.mjs?b318:1436:1)
at EffectScope.run (reactivity.esm-bundler.js?89dc:34:1)
at eval (pinia.mjs?b318:1436:1)
at EffectScope.run (reactivity.esm-bundler.js?89dc:34:1)
at createSetupStore (pinia.mjs?b318:1434:1)
at createOptionsStore (pinia.mjs?b318:1224:1)
at useStore (pinia.mjs?b318:1708:1)
at setup (BoardBox.vue?169e:43:1)
How do I initialize current_node
with the value of root
in vue.js?
Is there any function like constructor where I can initialize the value of the variables?