--- title: pinia tags: vue --- # 安裝 ``` npm i pinia ``` # 啟用 ```typescript= import { createApp } from 'vue'; import App from './App.vue'; import { createPinia } from 'pinia'; const app = createApp(App); app.use(createPinia); app.mount('#app'); ``` # 定義store ```typescript= import { defineStore } from 'pinia'; import { ref } from 'vue'; export const useUserStore = defineStore('user', () => { const counter = ref(123); const add = () => { counter.value += 1; }; return { counter, add }; }); ``` # 使用store ```vue= <template> <h1>msg: {{ store.counter }}</h1> <div> <button @click="add">click</button> </div> </template> <script setup lang="ts"> import { ref } from 'vue'; import { useUserStore } from './store/userstore'; const store = useUserStore(); const add = () => { store.add(); }; </script> ```
×
Sign in
Email
Password
Forgot password
or
By clicking below, you agree to our
terms of service
.
Sign in via Facebook
Sign in via Twitter
Sign in via GitHub
Sign in via Dropbox
Sign in with Wallet
Wallet (
)
Connect another wallet
New to HackMD?
Sign up