if (!pinia._s.has(id)) { // creating the store registers it in `pinia._s` if (isSetupStore) { createSetupStore(id, setup, options, pinia); // 创建组合式风格的仓库 } else { createOptionsStore(id, options, pinia); // 创建选项式风格的仓库 } /* istanbul ignore else */ { // @ts-expect-error: not the right inferred type useStore._pinia = pinia; } }
functionsetup() { if (!initialState && (!hot)) { /* istanbul ignore if */ if (isVue2) { set(pinia.state.value, id, state ? state() : {}); } else { pinia.state.value[id] = state ? state() : {}; } } // avoid creating a state in pinia.state.value const localState = hot ? // use ref() to unwrap refs inside state TODO: check if this is still necessary toRefs(ref(state ? state() : {}).value) : toRefs(pinia.state.value[id]); returnassign(localState, actions, Object.keys(getters || {}).reduce((computedGetters, name) => { if (name in localState) { console.warn(`[🍍]: A getter cannot have the same name as another state property. Rename one of them. Found with "${name}" in store "${id}".`); } computedGetters[name] = markRaw(computed(() => { setActivePinia(pinia); // it was created just before const store = pinia._s.get(id); // allow cross using stores /* istanbul ignore if */ if (isVue2 && !store._r) return; // @ts-expect-error // return getters![name].call(context, context) // TODO: avoid reading the getter while assigning with a global variable return getters[name].call(store, store); })); return computedGetters; }, {})); }