使用 Vue3 + Pinia + PNPM + Vite 開發(fā)一個前端項目時,運行preview,報錯:
pinia Cannot read properties of undefined (reading '_s')
報錯的代碼是壓縮后的:
function we(e, t, n) {
let s, c;
const u = typeof t == "function";
typeof e == "string" ? ((s = e), (c = u ? n : t)) : ((c = e), (s = e.id));
function a(o, h) {
const f = ie();
return (
(o = o || (f ? ue(be, null) : null)),
o && F(o),
(o = H),
o._s.has(s) || (u ? V(s, t, c, o) : ve(s, c, o)), // 報錯位置
o._s.get(s)
);
}
return (a.$id = s), a;
}
大意像是讀取一個 store的名稱失敗導致。而我確認這個 store 已經(jīng)被注冊(defineStore)了。
翻閱 pinia 源碼,定位到位置:https://github.com/vuejs/pinia/blob/5c94d26e11f8d2ed0e01d5a615f8e014d799b4a0/packages/pinia/src/store.ts#L895-L966 ,不過依然不明確問題原因。
突然靈感乍現(xiàn),有新發(fā)現(xiàn),我這個工程是 pnpm workspace, 兩個包都依賴了 pinia,但版本不一致了。統(tǒng)一版本后再 build,問題解決。
那么,怎么保證不同的包的相同依賴版本一致呢?pnpm 給出了方案:https://pnpm.io/package_json#pnpmoverrides
浙公網(wǎng)安備 33010602011771號