Using Pinia standalone (CDN or UNPKG) mode #1051
-
I use pinia in several projects through npm, but now I have a micro project where I should use it a standalone (CDN) mode. I've found an unpkg reference to pinia, but can't find a way to do it. This is my index.html skeleton: <!DOCTYPE html>
<html>
<head>
<link href="https://fonts.googleapis.com/css?family=Roboto:100,300,400,500,700,900" rel="stylesheet">
<link href="https://cdn.jsdelivr.net/npm/@mdi/[email protected]/css/materialdesignicons.min.css" rel="stylesheet">
<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/vuetify.min.css" rel="stylesheet">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no, minimal-ui">
</head>
<body>
<div id="app">
<v-app>
<v-main>
<v-container>Hello world</v-container>
</v-main>
</v-app>
</div>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/vue.js"></script>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/vuetify.js"></script>
<script src="https://unpkg.com/pinia"></script>
<script>
// This won't work: not top level
// import { createPinia } from 'pinia'
new Vue({
el: '#app',
vuetify: new Vuetify(),
pinia // Doesn't work, either
})
Vue.use(createPinia()) // As this fails also
</script>
</body>
</html> Is there an example or tutorial using pania standalone? Thanks. |
Beta Was this translation helpful? Give feedback.
Answered by
soc221b
Feb 13, 2022
Replies: 2 comments 7 replies
-
<!DOCTYPE html>
<html>
<head>
<link href="https://fonts.googleapis.com/css?family=Roboto:100,300,400,500,700,900" rel="stylesheet">
<link href="https://cdn.jsdelivr.net/npm/@mdi/[email protected]/css/materialdesignicons.min.css" rel="stylesheet">
<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/vuetify.min.css" rel="stylesheet">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no, minimal-ui">
</head>
<body>
<div id="app">
<v-app>
<v-main>
<v-container>Hello world</v-container>
<button @click="increment">Count is: {{ value }}</button>
</v-main>
</v-app>
</div>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/vue.js"></script>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/vuetify.js"></script>
<script src="https://unpkg.com/@vue/composition-api"></script>
<script src="https://unpkg.com/vue-demi"></script>
<script src="https://unpkg.com/pinia"></script>
<script>
const useCounterStore = Pinia.defineStore('counter', {
state() {
return {
value: 0
}
},
actions: {
increment(state) {
this.value++
}
}
})
Vue.use(Pinia.PiniaVuePlugin)
new Vue({
el: '#app',
vuetify: new Vuetify(),
pinia: Pinia.createPinia(),
computed: {
...Pinia.mapState(useCounterStore, ['value'])
},
methods: {
...Pinia.mapActions(useCounterStore, ['increment'])
},
})
</script>
</body>
</html> |
Beta Was this translation helpful? Give feedback.
7 replies
Answer selected by
balage1551
-
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<link rel="icon" type="image/svg+xml" href="/vite.svg" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Vite + TS</title>
<style>
[v-cloak] {
display: none;
}
</style>
</head>
<body>
<div id="app" v-cloak>
<button @click="increment">increment</button>
<p>{{ count }}</p>
</div>
<script type="importmap">
{
"imports": {
"vue": "https://unpkg.com/vue@3/dist/vue.esm-browser.js",
"@vue/devtools-api": "https://unpkg.com/@vue/[email protected]/lib/esm/index.js",
"pinia": "https://unpkg.com/[email protected]/dist/pinia.esm-browser.js"
}
}
</script>
<script type="module">
import { createApp, ref } from 'vue'
import { createPinia, defineStore, storeToRefs } from 'pinia'
const useCounterStore = defineStore('counter', () => {
const count = ref(0)
function increment() {
count.value++
}
return { count, increment }
})
const app = createApp({
setup() {
const { count } = storeToRefs(useCounterStore())
const { increment } = useCounterStore()
return { count, increment }
}
})
const pinia = createPinia()
app.use(pinia)
app.mount('#app')
</script>
</body>
</html> |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@vue/composition-api
andvue-demi
.Pinia
to access PInia's APIs.