Created
June 30, 2021 13:07
-
-
Save posva/28604e178deb5d5c9ab6cea86232053b to your computer and use it in GitHub Desktop.
Example of Pinia being shared across two Vue 3 apps
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html lang="en"> | |
<head> | |
<meta charset="UTF-8" /> | |
<meta http-equiv="X-UA-Compatible" content="IE=edge" /> | |
<meta name="viewport" content="width=device-width, initial-scale=1.0" /> | |
<title>Two Vue Apps, One Pinia</title> | |
</head> | |
<body> | |
<script src="https://unpkg.com/[email protected]/dist/vue.global.js"></script> | |
<script src="https://unpkg.com/[email protected]/dist/pinia.global.js"></script> | |
<div id="app-one"> | |
Store: {{ store.$state }} | |
<button @click="store.n++">Increment</button> | |
</div> | |
<div id="app-two">Store: {{ store.$state }}</div> | |
<script> | |
const useStore = Pinia.defineStore({ | |
id: 'main', | |
state: () => ({ n: 0 }), | |
}) | |
const Page = { | |
setup() { | |
const store = useStore() | |
return { store } | |
}, | |
} | |
const pinia = Pinia.createPinia() | |
Vue.createApp({ ...Page }) | |
.use(pinia) | |
.mount('#app-one') | |
Vue.createApp({ ...Page }) | |
.use(pinia) | |
.mount('#app-two') | |
</script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment