我正在尝试分享一家 pinia 商店但没有成功。这篇文章中提到的问题 https://github.com/vuejs/pinia/discussions/1073 似乎仍然是最新的
这里是用来测试的上下文。
项目包含商店(prj02)
/lib.js
import { defineStore } from 'pinia'
export const useTestStore = defineStore('test', {
state: () => {
return { count: 0 }
},
actions: {
increment() {
this.count++
}
}
})
/package.json
{
"name": "prj02",
"private": true,
"version": "0.0.1",
"type": "module",
"files": ["dist"],
"main": "./dist/prj02.umd.cjs",
"module": "./dist/prj02.js",
"exports": {
".": {
"import": "./dist/prj02.js",
"require": "./dist/prj02.umd.cjs"
}
},
"scripts": {
"build": "vite build"
},
"peerDependencies": {
"pinia": "^2.0.33",
"vue": "^3.2.45"
},
"devDependencies": {
"@vitejs/plugin-vue": "^4.0.0",
"vite": "^4.1.0"
}
}
/vite.config.js
import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'
// https://vitejs.dev/config/
export default defineConfig({
plugins: [vue()],
build: {
lib: {
entry: './lib.js',
name: 'prj02',
fileName: 'prj02'
},
rollupOptions: {
external: ['vue', 'pinia'],
output: {
globals: {
vue: 'Vue',
pinia: 'Pinia'
}
}
}
}
})
项目使用商店(prj01)
/package.json
{
"name": "prj01",
"private": true,
"version": "0.0.1",
"type": "module",
"scripts": {
"dev": "vite",
"build": "vite build",
"preview": "vite preview"
},
"dependencies": {
"pinia": "^2.0.33",
"prj02": "file:../prj02",
"vue": "^3.2.45"
},
"devDependencies": {
"@vitejs/plugin-vue": "^4.0.0",
"vite": "^4.1.0"
}
}
/vite.config.js
import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'
// https://vitejs.dev/config/
export default defineConfig({
plugins: [vue()]
})
/index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>prj01</title>
</head>
<body>
<div id="app"></div>
<script type="module" src="/src/main.js"></script>
</body>
</html>
/src/main.js
import { createApp } from 'vue'
import { createPinia } from 'pinia'
import App from './App.vue'
const pinia = createPinia()
const app = createApp(App)
app.use(pinia)
app.mount('#app')
/src/App.vue
<script setup>
import { useTestStore } from 'prj02'
const store = useTestStore()
</script>
<template>
<button type="button" @click="store.increment()">inc pinia</button>
<div>{{ store.count }}</div>
</template>
prj01 在开发模式下按预期启动 (npm run dev),但在预览模式下失败并出现以下错误。 (与初始帖子中发现的错误相同)
index-539c89b0.js:1 TypeError: Cannot read properties of undefined (reading '_s')
at i (index-539c89b0.js:9:3078)
at setup (index-539c89b0.js:9:3258)
at Be (index-539c89b0.js:1:13194)
at Bi (index-539c89b0.js:1:47582)
at Hi (index-539c89b0.js:1:47367)
at me (index-539c89b0.js:1:37198)
at ge (index-539c89b0.js:1:37088)
at I (index-539c89b0.js:1:34015)
at Jn (index-539c89b0.js:1:42067)
at mount (index-539c89b0.js:1:32532)
欢迎任何帮助