安装:
javascript
npm install pinia
min.js中引入
javascript
import { createApp } from 'vue'
import App from './App.vue'
import { createPinia } from "pinia";
const pinia = createPinia();
const app = createApp(App);
app.use(pinia);
app.mount("#app");
创建仓库 store/user.js
javascript
import { defineStore } from "pinia"
export const useUsersStore = defineStore('users', {
// 其它配置项
state: () => {
return {
name: "张三",
}
};
},
getters: {
},
actions: {
}
})
使用:
javascript
<script setup>
import { useUsersStore } from "@/store/user.js";
const useUsers = useUsersStore();
console.log(useUsers.name); //张三
</script>