Skip to content

Instantly share code, notes, and snippets.

@posva
Created June 30, 2021 13:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save posva/28604e178deb5d5c9ab6cea86232053b to your computer and use it in GitHub Desktop.
Save posva/28604e178deb5d5c9ab6cea86232053b to your computer and use it in GitHub Desktop.
Example of Pinia being shared across two Vue 3 apps
<!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/vue@3.1.2/dist/vue.global.js"></script>
<script src="https://unpkg.com/pinia@2.0.0-beta.3/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